之前我问过this question,现在我遇到了另一个类似的JSON字符串问题。
这是我的JSON字符串:
{"responseObject":{"code":"0","message":"HI","url":"www.abc.com","token":"{\"token\":\"abcdef\",\"id\":123}"}}
这是我的dserialize代码,它不起作用:
var def = new
{
code = string.Empty,
message = string.Empty,
url= string.Empty,
token = new {token = string.Empty}
};
var response = JsonConvert.DeserializeAnonymousType(data, def);
var innerDef = new { token= string.Empty, id= string.Empty };
var deserializedInner = JsonConvert.DeserializeAnonymousType(response.token.token, innerDef);
答案 0 :(得分:2)
你有两个主要问题:
responseObject
。token
提供了错误的类型。它是一个包含JSON的字符串,而不是一个对象。这有效:
var def = new
{
responseObject = new
{
code = string.Empty,
message = string.Empty,
url = string.Empty,
token = string.Empty
}
};
var response = JsonConvert.DeserializeAnonymousType(data, def);
var innerDef = new { token = string.Empty, id = string.Empty };
var deserializedInner =
JsonConvert.DeserializeAnonymousType(response.responseObject.token, innerDef);
答案 1 :(得分:0)
令牌是一个字符串而不是一个对象。的 “令牌”:“强>