JSON 字符串的反序列化将返回空值
本文关键字:返回 空值 反序列化 字符串 JSON | 更新日期: 2023-09-27 18:32:04
这是 JSON 字符串 -
"{''"body''":[''"VAL1''",''"VAL2''"],''"head''":{''"result''":true,''"time''":3.859}}"
这些是我的课程——
[Serializable]
public class ResponseHead
{
public bool result {get; set;}
public float time {get; set;}
}
[Serializable]
public class ResponseBody
{
public string[] body {get; set;}
}
[Serializable]
public class ResponseObj
{
public ResponseBody body {get; set;}
public ResponseHead head { get; set; }
}
而代码——
JavaScriptSerializer serializer = new JavaScriptSerializer();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
responseText = streamReader.ReadToEnd();
}
ResponseObj response_obj = new ResponseObj();
ResponseHead rhead = new ResponseHead();
rhead = serializer.Deserialize<ResponseHead>(responseText); //not working
生成的响应头对象具有以下值:
result: false
time: 0.0
它无法正确映射值,但我不确定为什么。响应正文值输入正确。
请帮忙!
看起来您正在尝试读取ResponseObj
(这是 JSON 中的顶级对象),但编码为 ResponseHead
.以下应该有效:
var wholeObject = serializer.Deserialize<ResponseObj>(responseText);
rhead = wholeObject.head;