0

这是 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

生成的 ResponseHead 对象具有值:

result: false 
time: 0.0 

它无法正确映射值,但我不知道为什么。ResponseBody 值正确输入。

请帮忙!

4

1 回答 1

4

看起来您正在尝试读取ResponseObj(这是 JSON 中的顶级对象),但编码为ResponseHead. 以下应该工作:

var wholeObject = serializer.Deserialize<ResponseObj>(responseText);
rhead = wholeObject.head;
于 2012-04-23T16:31:52.337 回答