解析一些“时髦”的方法使用 .Net 和 C# 的 JSON 数据
我连接的网络服务发送一些以奇怪的方式组织的 JSON 数据作为对我们查询的响应(毫无疑问他们做错了,但我们无法更改它)。
我对 C# 很陌生,并且已成功使用 DataContract 和 DataContractJsonSerializer 反序列化一些更标准的 JSON 对象。
然而,我对我们得到的那些扭曲的 JSON 感到相当困惑。在我们的 java 客户端 (Android) 上,我们只是决定使用一个简单的 JSON 解析器来跳过它们插入的额外数组。我很想从经验丰富的开发人员那里得到一些关于如何反序列化这个扭曲的 JSON 的意见。
以下是获取一些用户详细信息的 Web 服务将发送的内容以及它应该对应的 C# 对象:
C#
class Buddy
{
public String Login { get; set; }
public String Password { get; set; }
public List<Purchase> { get; set; }
}
class Purchase
{
public Int64 ItemId { get; set; }
public Int32 Quantity { get; set; }
}
JSON
[
{
"buddy":
[
{
"login": "johndoe",
"password": "pwd",
"purchase_list":
[
{
"purchase":
[
{
"item_id": 1654,
"qty": 1
}
]
},
{
"purchase":
[
{
"item_id": 654,
"qty": 2
}
]
}
]
}
]
}
]
The webservices I am connecting to send as response to our queries some JSON data organised in a weird way (no doubt they have done it wrong, but we cannot change it).
I am quite new to C# and have managed to deserialize some more standard JSON objects using DataContract and DataContractJsonSerializer.
However, I am rather puzzled with those twisted JSON we are getting. On our java client (Android), we have simply decided to go with a simple JSON parser that skips the extra arrays they have inserted. I would love to get some opinion from experienced developers on how to get this twisted JSON deserialized.
Here is what a web service to get some user details would send and the C# object it is supposed to correspond to :
C#
class Buddy
{
public String Login { get; set; }
public String Password { get; set; }
public List<Purchase> { get; set; }
}
class Purchase
{
public Int64 ItemId { get; set; }
public Int32 Quantity { get; set; }
}
JSON
[
{
"buddy":
[
{
"login": "johndoe",
"password": "pwd",
"purchase_list":
[
{
"purchase":
[
{
"item_id": 1654,
"qty": 1
}
]
},
{
"purchase":
[
{
"item_id": 654,
"qty": 2
}
]
}
]
}
]
}
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里介绍的库对于处理 json 非常有效:
http://james.newtonking.com/pages/json-net.aspx
The library introduced here is quite effective for handling json :
http://james.newtonking.com/pages/json-net.aspx