我正在使用RESTful JSON API,并尝试使用Newtonsoft的JSON.Net框架反序列化JSON响应消息的PART。
基本上,我收到的响应JSON消息包括头信息以及一组项(Cards)。当我试图反序列化到一个卡列表时,会抛出一个.JsonSerializationException。
我几乎可以肯定这是因为.DeserializeObject
方法在标头信息上出错。为了测试这个理论,我"按摩"了JSON响应消息&删除了标头信息;将剩余的JSON保存为字符串&将其传递给Deserialize方法,&成功了!
我的问题是-是否可以向.DeserializeObject
方法传递一个参数&强制它ONL反序列化JSON响应的数据组件?
代码:
public List<Cards.CardResponse> GetByUserToken(string UserToken)
{
string requestUrl = URL + "/user/" + UserToken;
HttpWebRequest request =(HttpWebRequest)WebRequest.Create(requestUrl);
request.Method = "GET";
request.ContentType = "application/json";
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
request.Headers.Add("Authorization", "Basic " + encoded);
List<Cards.CardResponse> ReturnCards = null;
try
{
// Get JSON response message
string jsonResponse = string.Empty;
HttpWebResponse ws = (HttpWebResponse)request.GetResponse();
using (System.IO.StreamReader sreader = new System.IO.StreamReader(ws.GetResponseStream()))
{
jsonResponse = sreader.ReadToEnd();
}
// *** THE LINE BELOW THROWS THE ERROR:
ReturnCards = (List<Cards.CardResponse>)JsonConvert.DeserializeObject(jsonResponse, typeof(List<Cards.CardResponse>));**
return ReturnCards;
错误消息:
Newtonsoft.Json.dll 中发生类型为"Newtonsoft.Json.JsonSerializationException
"的未处理异常
附加信息:
无法反序列化当前JSON对象(例如{"name":"value"})进入类型'System.Collections.Generic.List`1[ExenseWorks.Marqeta.Cards+CardResponse]'因为该类型需要JSON数组(例如[1,2,3])来反序列化正确地
要修复此错误,请将JSON更改为JSON数组(例如。[1,2,3])或更改反序列化的类型,使其成为正常的.NET类型(例如,不是像integer这样的基元类型,不是集合类型像数组或列表),可以从JSON对象反序列化。JsonObjectAttribute也可以添加到类型中,以强制它从JSON对象反序列化。
原始JSON:
我还包括了原始的JSON响应——基本上,我想弄清楚如何只从"数据"开始反序列化:
{
"count": 5,
"start_index": 0,
"end_index": 4,
"is_more": true,
"data": [{
"token": "9bd70529-f84a-406e-ad68-eabf69af690f",
"pan": "******______****",
"expiration": "0219",
"state": "UNACTIVATED",
"user_token": "2dea06b1-9fc8-423c-bb5b-b733ec895b38",
"card_product_token": "dfd110cf-a833-4b21-b7c1-b3bc62b97c52",
"last_four": "7281",
"pin_is_set": false,
"state_reason": "New card",
"fulfillment_status": "ISSUED"
},
{
"token": "0ec53aef-768d-4499-a661-9e26172b8369",
"pan": "******______****",
"expiration": "0219",
"state": "UNACTIVATED",
"user_token": "2dea06b1-9fc8-423c-bb5b-b733ec895b38",
"card_product_token": "dfd110cf-a833-4b21-b7c1-b3bc62b97c52",
"last_four": "5216",
"pin_is_set": false,
"state_reason": "New card",
"fulfillment_status": "ISSUED"
},
{
"token": "82c8b386 -f4b6-40d9-bee6-dd0a5b74d55b",
"pan": "******______****",
"expiration": "0219",
"state": "UNACTIVATED",
"user_token": "2dea06b1-9fc8-423c-bb5b-b733ec895b38",
"card_product_token": "dfd110cf-a833-4b21-b7c1-b3bc62b97c52",
"last_four": "6640",
"pin_is_set": false,
"state_reason": "New card",
"fulfillment_status": "ISSUED"
},
{
"token": "1181f23d-b464-4af2-95d9-8b8f48c4d6a8",
"pan": "******______****",
"expiration": "0219",
"state": "UNACTIVATED",
"user_token": "2dea06b1-9fc8-423c-bb5b-b733ec895b38",
"card_product_token": "dfd110cf-a833-4b21-b7c1-b3bc62b97c52",
"last_four": "3390",
"pin_is_set": false,
"state_reason": "New card",
"fulfillment_status": "ISSUED"
},
{
"token": "21870467-b059-472e-a130-938356ff1f4a",
"pan": "******______****",
"expiration": "0219",
"state": "UNACTIVATED",
"user_token": "2dea06b1-9fc8-423c-bb5b-b733ec895b38",
"card_product_token": "dfd110cf-a833-4b21-b7c1-b3bc62b97c52",
"last_four": "7387",
"pin_is_set": false,
"state_reason": "New card",
"fulfillment_status": "ISSUED"
}]
}
您可以先将其反序列化为JObject,然后提取所需的部分:
var json = JObject.Parse(jsonString);
var partialJsonString = JsonConvert.SerializeObject(json["data"]);
var yourObject = JsonConvert.DeserializeObject<YourType>(partialJsonString );
然而,为所有内容创建类是非常琐碎的:
public class ReplyRoot
{
public int count { get; set; }
public int start_index { get; set; }
public int end_index { get; set; }
public int is_more { get; set; }
public DataEntry[] data { get; set; }
}
public class DataEntry
{
public string token { get; set; }
// [.. all properties ..]
}
var dto = JsonConvert.DeserializeObject<ReplyRoot>(jsonString);
您可以创建一个新的类来保存整个JSON对象
public class CardResponseWrapper
{
public int count { get; set; }
public int startIndex { get; set; }
public int endIndex { get; set; }
public bool is_more { get; set; }
public List<Cards.CardResponse> data { get; set; }
}
然后将请求的结果投射到此对象:
var result = (CardResponseWrapper)JsonConvert.DeserializeObject(jsonResponse, typeof(CardResponseWrapper));
这是您的json格式:
{
"count": 5,
"start_index": 0,
"end_index": 4,
"is_more": true,
"data": [{
"token": "9bd70529-f84a-406e-ad68-eabf69af690f",
"pan": "******______****",
"expiration": "0219",
"state": "UNACTIVATED",
"user_token": "2dea06b1-9fc8-423c-bb5b-b733ec895b38",
"card_product_token": "dfd110cf-a833-4b21-b7c1-b3bc62b97c52",
"last_four": "7281",
"pin_is_set": false,
"state_reason": "New card",
"fulfillment_status": "ISSUED"
}, {
"token": "0ec53aef-768d-4499-a661-9e26172b8369",
"pan": "******______****",
"expiration": "0219",
"state": "UNACTIVATED",
"user_token": "2dea06b1-9fc8-423c-bb5b-b733ec895b38",
"card_product_token": "dfd110cf-a833-4b21-b7c1-b3bc62b97c52",
"last_four": "5216",
"pin_is_set": false,
"state_reason": "New card",
"fulfillment_status": "ISSUED"
}, {
"token": "82c8b386 -f4b6-40d9-bee6-dd0a5b74d55b",
"pan": "******______****",
"expiration": "0219",
"state": "UNACTIVATED",
"user_token": "2dea06b1-9fc8-423c-bb5b-b733ec895b38",
"card_product_token": "dfd110cf-a833-4b21-b7c1-b3bc62b97c52",
"last_four": "6640",
"pin_is_set": false,
"state_reason": "New card",
"fulfillment_status": "ISSUED"
}, {
"token": "1181f23d-b464-4af2-95d9-8b8f48c4d6a8",
"pan": "******______****",
"expiration": "0219",
"state": "UNACTIVATED",
"user_token": "2dea06b1-9fc8-423c-bb5b-b733ec895b38",
"card_product_token": "dfd110cf-a833-4b21-b7c1-b3bc62b97c52",
"last_four": "3390",
"pin_is_set": false,
"state_reason": "New card",
"fulfillment_status": "ISSUED"
}, {
"token": "21870467-b059-472e-a130-938356ff1f4a",
"pan": "******______****",
"expiration": "0219",
"state": "UNACTIVATED",
"user_token": "2dea06b1-9fc8-423c-bb5b-b733ec895b38",
"card_product_token": "dfd110cf-a833-4b21-b7c1-b3bc62b97c52",
"last_four": "7387",
"pin_is_set": false,
"state_reason": "New card",
"fulfillment_status": "ISSUED"
}]
}
如果这是您的Json:
{
n"count": 5,
n"start_index": 0,
n"end_index": 4,
n"is_more": true,
n"data": [
{
n"token": "9bd70529-f84a-406e-ad68-eabf69af690f",
n"pan": "******______****",
n"expiration": "0219",
n"state": "UNACTIVATED",
n"user_token": "2dea06b1-9fc8-423c-bb5b-b733ec895b38",
n"card_product_token": "dfd110cf-a833-4b21-b7c1-b3bc62b97c52",
n"last_four": "7281",
n"pin_is_set": false,
n"state_reason": "Newcard",
n"fulfillment_status": "ISSUED"n
},
{
n"token": "0ec53aef-768d-4499-a661-9e26172b8369",
n"pan": "******______****",
n"expiration": "0219",
n"state": "UNACTIVATED",
n"user_token": "2dea06b1-9fc8-423c-bb5b-b733ec895b38",
n"card_product_token": "dfd110cf-a833-4b21-b7c1-b3bc62b97c52",
n"last_four": "5216",
n"pin_is_set": false,
n"state_reason": "Newcard",
n"fulfillment_status": "ISSUED"n
},
{
n"token": "82c8b386-f4b6-40d9-bee6-dd0a5b74d55b",
n"pan": "******______****",
n"expiration": "0219",
n"state": "UNACTIVATED",
n"user_token": "2dea06b1-9fc8-423c-bb5b-b733ec895b38",
n"card_product_token": "dfd110cf-a833-4b21-b7c1-b3bc62b97c52",
n"last_four": "6640",
n"pin_is_set": false,
n"state_reason": "Newcard",
n"fulfillment_status": "ISSUED"n
},
{
n"token": "1181f23d-b464-4af2-95d9-8b8f48c4d6a8",
n"pan": "******______****",
n"expiration": "0219",
n"state": "UNACTIVATED",
n"user_token": "2dea06b1-9fc8-423c-bb5b-b733ec895b38",
n"card_product_token": "dfd110cf-a833-4b21-b7c1-b3bc62b97c52",
n"last_four": "3390",
n"pin_is_set": false,
n"state_reason": "Newcard",
n"fulfillment_status": "ISSUED"n
},
{
n"token": "21870467-b059-472e-a130-938356ff1f4a",
n"pan": "******______****",
n"expiration": "0219",
n"state": "UNACTIVATED",
n"user_token": "2dea06b1-9fc8-423c-bb5b-b733ec895b38",
n"card_product_token": "dfd110cf-a833-4b21-b7c1-b3bc62b97c52",
n"last_four": "7387",
n"pin_is_set": false,
n"state_reason": "Newcard",
n"fulfillment_status": "ISSUED"n
}
]n
}
这不是要解析的有效json。您可以在以下位置验证:http://jsonlint.com/