C#复杂的Json反序列化

时间:2015-08-20 03:12:50

标签: c# json deserialization

我试图反序化这个json,但这是复杂的json,我不能。实际上我试图在http://json2csharp.com/中创建一个类,但它不再起作用。你能帮我搞清楚吗?

    {

    "?xml":{

    "@version":"1.0",

    "@encoding":"utf-8"

    },

    "ArrayOfPerson":{

    "@xmlns:xsd":"http://www.w3.org/2001/XMLSchema",

    "@xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance",

    "alert":[

    {

    "@xmlns":"urn:url:taces:numarates",

    "identifier":"489756",

    "sender":"example.com",

    "sent":"2015-08-19T23:44:00+00:00",

    "status":"Actual",

    "msgType":"wait",

    "source":"Company",

    "scope":"Public",

    "info":{

    "category":"known",

    "event":"event created",

    "responseType":"Execute",

    "urgency":"Expected",

    "severity":"Severe",

    "certainty":"Likely",

    "effective":"2015-08-19T23:44:00+00:00",

    "expires":"2015-08-20T00:29:00+00:00",

    "senderName":"Company",

    "headline":"headlineExample",

    "description":" description string ",

    "instruction":" instruction description.",

    "contact":"mail adress",

    "parameter":[

    {
    "valueName":"city",
    "value":"city time"
    },

    {
    "valueName":"severity",
    "value":"High"
    },

    {
    "valueName":"POLYGON",
    "value":"POLYGON ((25.8600349687548 -100.744518850353, 25.8335161877637 
    -100.664518850353, 25.8346209950992 -100.624518850353, 25.8442798845219 
    -100.61828081562, 25.8842798845219 -100.602879447141, 25.9242798845219 
    -100.604707951971, 25.941798102096 -100.624518850353, 25.9243730812152 
    -100.684518850353, 25.9242798845219 -100.684661516991, 25.9095974027013 
    -100.704518850353, 25.9042798845219 -100.710343745251, 25.8642798845219 
    -100.747917201708, 25.8600349687548 -100.744518850353))"
    },

    {
    "valueName":"direction",
    "value":"2"
    },

    {
    "valueName":"speed",
    "value":"27 km"
    }

    ],

    "area":{
    "areaDesc":"Latitude: 25.885, Longitude: -100.664",
    "polygon":"26.1256671949002 -100.814556141911, 25.5230724567197 
    -100.823124650158, 25.5734941016972 -100.397349654998, 26.1574195901602 
    -100.546429706446, 26.1256671949002 -100.814556141911"
    }

    }

    }

    ... array items

    ]
    }
    }

1 个答案:

答案 0 :(得分:0)

似乎您的问题是因为您的JSON属性名称不能用作C#变量"因为"因此,json2charp返回的名称类似于" invalid-name __ @ version"

几乎所有Json desializers包括DataContractJsonSerializers和Json.Net都可以支持自定义数据管理名称。它不需要与您班级中的属性名称相同。请参阅DataMember attribute on MSDN

或者,您可以使用Json.Net库和JsonReader按名称http://insidethecpu.com/2013/06/19/json-parsing/手动寻址属性