C# 复杂 Json 反序列化
C# Complicated Json Deserialize
我正在尝试反序列化此 json 但这很复杂 json 而我做不到。实际上,我试图在 http://json2csharp.com/ 中创建一个 class,但它再次不起作用。你能帮我弄清楚吗
{
"?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
]
}
}
您的问题似乎是因为您的 JSON 属性 名称不能用作 C# 变量 "as is" 因此 json2charp returns 名称如 "invalid-name__@version"
几乎所有 Json 反序列化器,包括 DataContractJsonSerializers 和 Json.Net 都可以支持自定义数据成员名称。它不需要与 class 中的 属性 名称相同。参考DataMember attribute on MSDN
或者,您可以使用 Json.Net 库和 JsonReader 按名称手动寻址属性 http://insidethecpu.com/2013/06/19/json-parsing/
我正在尝试反序列化此 json 但这很复杂 json 而我做不到。实际上,我试图在 http://json2csharp.com/ 中创建一个 class,但它再次不起作用。你能帮我弄清楚吗
{
"?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
]
}
}
您的问题似乎是因为您的 JSON 属性 名称不能用作 C# 变量 "as is" 因此 json2charp returns 名称如 "invalid-name__@version"
几乎所有 Json 反序列化器,包括 DataContractJsonSerializers 和 Json.Net 都可以支持自定义数据成员名称。它不需要与 class 中的 属性 名称相同。参考DataMember attribute on MSDN
或者,您可以使用 Json.Net 库和 JsonReader 按名称手动寻址属性 http://insidethecpu.com/2013/06/19/json-parsing/