Ala*_*an2 0 c# json deserialization
我有以下内容:
var result2 = result1
.Select((t, index) => new {
Answer = t.Answer,
Answers = JSON.FromJSONString<Answer2>(t.AnswerJSON)
});
return Ok(result2);
public class Answer2 {
public bool? Correct; // Maybe this should be a property
public bool Response; // Maybe this should be a property
}
Run Code Online (Sandbox Code Playgroud)
我的String>对象函数:
public static T FromJSONString<T>(this string obj) where T : class
{
if (obj == null)
{
return null;
}
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(obj)))
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
T ret = (T)ser.ReadObject(stream);
return ret;
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法解决这个问题,如果JSON字符串中的Response存在null或者JSON字符串中没有Response值,我可以将Response字段设为false?
注意:我有一个关于使用房产的建议,我认为这可行,但我不确定如何在实践中这样做.
你应该使用一个属性来解决这个问题:
public class Answer2 {
private bool correct; // This field has no need to be nullable
public bool? Correct
{
get { return correct; }
set { correct = value.GetValueOrDefault(); }
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
87 次 |
| 最近记录: |