将部分JSON反序列化为C#对象 [英] Deserialize partial json to c# object

查看:92
本文介绍了将部分JSON反序列化为C#对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Json中有客户数据,每个客户都有3或4个地址.每个地址的参数名称均为diff,但类结构保持不变.

即送货地址和帐单地址将具有相同的地址和电话号码结构.因此,不要创建像billingaddress和shippingaddress这样的差异地址类.

我们可以创建一个将由所有人使用的单一地址类吗?

我尝试创建自定义jsonconverter.但是TOObject()最终调用readjson方法,并且进入无限循环.一个示例代码片段会有所帮助.

谢谢.

下面是我的样本杰森.

{
"CustomerName":"ABC",
性别":女",
"Samedaydelivery":"Y",
"OrderPrice":"123.12",
"addressinfo":{
"maincust":{
"maincustaddress":[{
名称":"XXX",
"Addressline1":"flatnumber",
"Addressline2":"Streetname",
城市":伦敦",
邮政编码":" 123123"
}],
"Maincontact":[{
"firstname":"Q",
姓氏":"A",
联系人":[{
"type":"EMAIL",
电子邮件":"test@test.com",
"number":null
},{

数字":"1231231231",
"type":"PHONE",
电子邮件":null
}]
}]
},
"billinginfo":{
"billaddress":[{
名称":"XXX",
"Addressline1":"flatnumber",
"Addressline2":"Streetname",
城市":伦敦",
邮政编码":" 123123"
}],
"billingcontact":[{
"firstname":"Q",
姓氏":"A",
联系人":[{
"type":"EMAIL",
电子邮件":"test@test.com",
"number":null
},{

数字":"1231231231",
"type":"PHONE",
电子邮件":null
}]
}]
},
"shippinginfo":{
"shippingaddress":[{
名称":"XXX",
"Addressline1":"flatnumber",
"Addressline2":"Streetname",
城市":伦敦",
邮政编码":" 123123"
}],
"ship_contact":[{
"firstname":"A",
姓":"Z",
联系人":[{
"type":"EMAIL",
电子邮件":"test@test.com",
"number":null
},{

数字":"1231231231",
"type":"PHONE",
电子邮件":null
}]
}]
}
}
}

解决方案

使用http://json2csharp.com/创建C#类rom json


I have customer data in Json and there will be 3 or 4 addresses associated to each customer. The parameter name is diff for each address but the class structure remains same.

that is shipping and billing address will have same structure for address and phone number. So instead of creating diff address class like billingaddress and shippingaddress.

Can we create a single class Address which will b used by all.

I tried with creating custom jsonconverter. But the TOObject() ends up calling readjson method and it goes in infinite loop. A sample code snippet would help.

Thanks.

Below is my sample Json.

{
"CustomerName": "ABC",
"Sex": "Female",
"Samedaydelivery": "Y",
"OrderPrice": "123.12",
"addressinfo": {
"maincust": {
"maincustaddress": [{
"Name": "XXX",
"Addressline1": "flatnumber",
"Addressline2": "Streetname",
"city": "London",
"zipcode": "123123"
}],
"Maincontact": [{
"firstname": "Q",
"lastname": "A",
"contacts": [{
"type": "EMAIL",
"email": "test@test.com",
"number": null
}, {

"number": "1231231231",
"type": "PHONE",
"email": null
}]
}]
},
"billinginfo": {
"billaddress": [{
"Name": "XXX",
"Addressline1": "flatnumber",
"Addressline2": "Streetname",
"city": "London",
"zipcode": "123123"
}],
"billingcontact": [{
"firstname": "Q",
"lastname": "A",
"contacts": [{
"type": "EMAIL",
"email": "test@test.com",
"number": null
}, {

"number": "1231231231",
"type": "PHONE",
"email": null
}]
}]
},
"shippinginfo": {
"shippingaddress": [{
"Name": "XXX",
"Addressline1": "flatnumber",
"Addressline2": "Streetname",
"city": "London",
"zipcode": "123123"
}],
"ship_contact": [{
"firstname": "A",
"lastname": "Z",
"contacts": [{
"type": "EMAIL",
"email": "test@test.com",
"number": null
}, {

"number": "1231231231",
"type": "PHONE",
"email": null
}]
}]
}
}
}

解决方案

Use http://json2csharp.com/ to create c# class rom json


这篇关于将部分JSON反序列化为C#对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆