如何在C#中反序列化奇怪的JSON格式

时间:2018-05-18 08:26:44

标签: c# json

我从服务器获得此JSON响应:

"{\"Data\":[{\"AI\":\"(00)103002310000531111\",\"BatchId\":157,\"LogisticLevel\":7,\"ParentId\":-1,\"State\":1,\"SyncState\":-1,\"InternalID\":86996,\"ModifyReason\":null,\"AggregationDate\":\"1900-01-01T00:00:00\",\"CommissioningDate\":\"1900-01-01T00:00:00\",\"DecommissioningDate\":\"1900-01-01T00:00:00\",\"NumberOfChildren\":0,\"RejectCode\":-1,\"ShippingDate\":\"1900-01-01T00:00:00\",\"TotalNumberOfUnits\":1,\"CompanyPrefix\":\"030023\",\"FilterValue\":7,\"PackLevel\":1,\"ReferenceCode\":\"\",\"Schema\":1,\"SerialNumber\":\"1000053111\",\"IsGood\":true,\"Children\":[]}],\"Code\":10,\"Message\":\"Data retrieved\"}"

enter image description here

我的责任是错的......大幅削减......为什么?

我使用Newtonsoft JSON序列化数据。 但格式不正确,我在客户端反序列化时收到错误。 我看到JSON字符串中有斜杠。我怎么解决这个问题? 我用C#。

2 个答案:

答案 0 :(得分:3)

您的Json是有效的,可以使用JsonConvert进行反序列化:

public class Datum
{
    public string AI { get; set; }
    public int BatchId { get; set; }
    public int LogisticLevel { get; set; }
    public int ParentId { get; set; }
    public int State { get; set; }
    public int SyncState { get; set; }
    public int InternalID { get; set; }
    public object ModifyReason { get; set; }
    public DateTime AggregationDate { get; set; }
    public DateTime CommissioningDate { get; set; }
    public DateTime DecommissioningDate { get; set; }
    public int NumberOfChildren { get; set; }
    public int RejectCode { get; set; }
    public DateTime ShippingDate { get; set; }
    public int TotalNumberOfUnits { get; set; }
    public string CompanyPrefix { get; set; }
    public int FilterValue { get; set; }
    public int PackLevel { get; set; }
    public string ReferenceCode { get; set; }
    public int Schema { get; set; }
    public string SerialNumber { get; set; }
    public bool IsGood { get; set; }
    public List<object> Children { get; set; }
}

public class RootObject
{
    public List<Datum> Data { get; set; }
    public int Code { get; set; }
    public string Message { get; set; }
}

JsonConvert.DeserializeObject<RootObject>(
    "{\"Data\":[{\"AI\":\"(00)103002310000531111\",\"BatchId\":157,\"LogisticLevel\":7,\"ParentId\":-1,\"State\":1,\"SyncState\":-1,\"InternalID\":86996,\"ModifyReason\":null,\"AggregationDate\":\"1900-01-01T00:00:00\",\"CommissioningDate\":\"1900-01-01T00:00:00\",\"DecommissioningDate\":\"1900-01-01T00:00:00\",\"NumberOfChildren\":0,\"RejectCode\":-1,\"ShippingDate\":\"1900-01-01T00:00:00\",\"TotalNumberOfUnits\":1,\"CompanyPrefix\":\"030023\",\"FilterValue\":7,\"PackLevel\":1,\"ReferenceCode\":\"\",\"Schema\":1,\"SerialNumber\":\"1000053111\",\"IsGood\":true,\"Children\":[]}],\"Code\":10,\"Message\":\"Data retrieved\"}"

).Dump();

答案 1 :(得分:1)

String json= "{\"Data\":[{\"AI\":\"(00)103002310000531111\",\"BatchId\":157,\"LogisticLevel\":7,\"ParentId\":-1,\"State\":1,\"SyncState\":-1,\"InternalID\":86996,\"ModifyReason\":null,\"AggregationDate\":\"1900-01-01T00:00:00\",\"CommissioningDate\":\"1900-01-01T00:00:00\",\"DecommissioningDate\":\"1900-01-01T00:00:00\",\"NumberOfChildren\":0,\"RejectCode\":-1,\"ShippingDate\":\"1900-01-01T00:00:00\",\"TotalNumberOfUnits\":1,\"CompanyPrefix\":\"030023\",\"FilterValue\":7,\"PackLevel\":1,\"ReferenceCode\":\"\",\"Schema\":1,\"SerialNumber\":\"1000053111\",\"IsGood\":true,\"Children\":[]}],\"Code\":10,\"Message\":\"Data retrieved\"}";

 dynamic bsObj = JsonConvert.DeserializeObject<dynamic>(json);
 Console.WriteLine(bsObj.ToString());
 Console.WriteLine(bsObj.Data[0].BatchId.ToString());  //157
 Console.WriteLine(bsObj.Code.ToString()); // 10
 Console.WriteLine(bsObj.Message.ToString()); // Data retrieved