2015-02-24 69 views
0

我正在使用RESTful JSON API,并试图使用Newtonsoft的JSON.Net框架反序列化部分JSON响应消息。Deserialise JSON响应的一部分

基本上,我接收到的回复JSON消息包括标题信息以及项目数组(卡片)。当我尝试反序列化到一张卡片列表时,抛出了.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.JsonSerializationException' 的未处理的异常发生在Newtonsoft.Json.dll

其他信息:

无法反序列化当前的JSON对象(例如, {“name”:“value”}) 转换成 'System.Collections.Generic.List`1 [ExpenseWorks.Marqeta.Cards + CardResponse]' 因为该类型需要一个JSON数组(例如[1,2,3 ])正确地反序列化 。

为了解决这个错误或者改变JSON到JSON阵列(例如 [1,2,3]),或改变它的反序列化类型,以便它是一个正常的.NET 类型(例如不是原始类型像整数,而不是类似数组或列表的集合类型 ),它们可以从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" 
    }] 
} 

回答

-1

如果这是你的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解析。 您可以verificate这一点:http://jsonlint.com/

+0

这是一条评论,而不是答案。 – jgauffin 2015-02-24 12:42:12

+0

我无法评论,对不起。另外,如果认为这是一个有效的答案。 – 2015-02-24 12:44:30

+0

很明显,字符串中存在转义字符。如果你删除它们,它是有效的JSON。检查你自己,我在他的回答中没有转义JSON。 – jgauffin 2015-02-24 12:47:47

2

您可以先将其反序列化到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); 
+0

你也可以使用“动态”来解析或使用反序列化属性来忽略你不想要的东西 – Stephan 2015-02-24 12:49:16

+0

辉煌 - 谢谢!反序列化到JObject看起来对我们来说是最好的解决方案;我们不必将标题字段添加到我们现有的类中。再次 - 谢谢! – MicP 2015-02-24 20:03:27

+0

np。很高兴它有帮助。 – jgauffin 2015-02-24 20:04:44

0

你可以做一个新的类来保存整个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" 
    }] 
}