反序列化Json对字典的响应<string,object =“”>抛出错误:反序列化对象时意外结束

时间:2016-02-09 15:40:18

标签: c# json json-deserialization

您好我正在尝试反序列化json响应,如下所示

{     
    [ 
        { 
            "WebCash" : { 
                "Id" : 1021,
                "RedemptionCode" : "sdfkjk",
                "PlayerCardId" : "3802",
                "Amount" : 8000,
                "Status" : 1,
                "PurchaseTimeStamp" : 1445020270,
                "RedeemTimeStamp" : 1445021971,
                "RetailerId" : "1781",
                "TerminalId" : "9",
            }, { 
                "Id" : 1160,
                "RedemptionCode" : "9123LKBJFDAXEK8194",
                "PlayerCardId" : "3802",
                "Amount" : 10000,
                "Status" : 3,
                "PurchaseTimeStamp" : 1445020270,
                "RetailerId" : "1781",
                "TerminalId" : "9",
            }
       } 
    ]
}

如果我们的响应类型为{[{,,},{,,}}}

,那么当我们没有不同的块时它会正常工作但会抛出错误

我正在使用以下代码

String Expected_Response = Response;
Dictionary<String, Object> ActualResponse_Dic = null; 
ActualResponse_Dic = JsonConvert.DeserializeObject<Dictionary<String, Object>>(ResponseValue);  //ResponseValue holds the entire Json response string

它在最后一行抛出错误 反序列化对象路径时出现意外结束....

2 个答案:

答案 0 :(得分:0)

我同意艾米提供的评论。请尝试以下方法:

  • 删除最外面的大括号{}
  • 删除“TerminalId”之后的逗号:“9”
  • 在“WebCash”对象周围放置方括号[]:

也许如下所示:

[{
"WebCash": [{
    "Id": 1021,
    "RedemptionCode": "sdfkjk",
    "PlayerCardId": "3802",
    "Amount": 8000,
    "Status": 1,
    "PurchaseTimeStamp": 1445020270,
    "RedeemTimeStamp": 1445021971,
    "RetailerId": "1781",
    "TerminalId": "9"
}, {
    "Id": 1160,
    "RedemptionCode": "9123LKBJFDAXEK8194",
    "PlayerCardId": "3802",
    "Amount": 10000,
    "Status": 3,
    "PurchaseTimeStamp": 1445020270,
    "RetailerId": "1781",
    "TerminalId": "9"
}]
}]

答案 1 :(得分:0)

您的JSON无效。另外,字典&lt;字符串,对象&gt; 可能会序列化为这样的内容:

    { 
        "WebCash" : { 
            "Id" : 1021,
            "RedemptionCode" : "sdfkjk",
            "PlayerCardId" : "3802",
            "Amount" : 8000,
            "Status" : 1,
            "PurchaseTimeStamp" : 1445020270,
            "RedeemTimeStamp" : 1445021971,
            "RetailerId" : "1781",
            "TerminalId" : "9"
        }, 
        "WebCash2" : { 
            "Id" : 1160,
            "RedemptionCode" : "9123LKBJFDAXEK8194",
            "PlayerCardId" : "3802",
            "Amount" : 10000,
            "Status" : 3,
            "PurchaseTimeStamp" : 1445020270,
            "RetailerId" : "1781",
            "TerminalId" : "9"
        }
   }