反序列化JSON响应的一部分

时间:2015-02-24 12:33:40

标签: c# json json.net json-deserialization

我正在使用RESTful JSON API,并且正在尝试使用Newtonsoft的JSON.Net框架反序列化JSON响应消息的PART。

基本上,我收到的响应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.dll中出现类型为“Newtonsoft.Json.JsonSerializationException”的未处理异常

其他信息:

  

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

     

要修复此错误,请将JSON更改为JSON数组(例如   [1,2,3])或更改反序列化类型,使其成为正常的.NET   type(例如,不是像整数这样的基本类型,不是集合类型   像数组或List一样,可以从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"
    }]
}

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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"
    }]
}

答案 2 :(得分:-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解析。 您可以在http://jsonlint.com/

中对此进行验证