我以前问过这个问题,现在我遇到了另一个类似JSON字符串的问题。
这是我的JSON字符串:
{"responseObject":{"code":"0","message":"HI","url":"www.abc.com","token":"{"token":"abcdef","id":123}"}}
这是我的数据序列化代码,它不起作用:
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);
您遇到了两个主要问题:
- 您没有考虑
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);
Token是一个字符串,而不是一个对象"token":"