我从Windows phone8的web服务中获得了一些json代码。多亏了站点json2csharp (http://json2csharp.com/),我才能生成我的entities类。但是有一个web服务,它有奇怪的json代码,就像这样。编号为键(0,1,2):
{
"service": "MainMapService",
"func": "getPlacesByAxes",
"result": {
"0": {
"id": "13478",
"date": "0",
"id_cat": "1",
"id_cat_sub": "0",
"id_user": "0",
},
"2": {
"id": "23272",
"date": "0",
"id_cat": "1",
"id_cat_sub": "0",
"id_user": "667"
},
"1": {
"id": "21473",
"date": "0",
"id_cat": "1",
"id_cat_sub": "0",
"id_user": "0"
}
},
"args": [
"1",
"50.8",
"4.5",
"1"
]
}json2csharp生成的类如下所示...每个类对应一个数字:
public class __invalid_type__0
{
public string id { get; set; }
public string date { get; set; }
public string id_cat { get; set; }
public string id_cat_sub { get; set; }
public string id_user { get; set; }
}
public class __invalid_type__2
{
public string id { get; set; }
public string date { get; set; }
public string id_cat { get; set; }
public string id_cat_sub { get; set; }
public string id_user { get; set; }
}
public class __invalid_type__1
{
public string id { get; set; }
public string date { get; set; }
public string id_cat { get; set; }
public string id_cat_sub { get; set; }
public string id_user { get; set; }
}
public class Result
{
public __invalid_type__0 __invalid_name__0 { get; set; }
public __invalid_type__2 __invalid_name__2 { get; set; }
public __invalid_type__1 __invalid_name__1 { get; set; }
}
public class RootObject
{
public string service { get; set; }
public string func { get; set; }
public Result result { get; set; }
public List<string> args { get; set; }
}所以,问题出在编号键上,可能有几个数字。你知道我该怎么解决这个问题吗?我不能更改Json代码...
提前谢谢你
发布于 2013-04-17 23:05:34
这并不优雅,但你可以试一试。
那么,我的想法是:
我创建了两个类
RootObject帮助器
公共类YourJsonClass {公共字符串服务{ get;set;}公共字符串函数{ get;set;}公共动态结果{ get;set;}公共string[]参数{ get;set;}}
result帮助器
public class Item
{
public string id { get; set; }
public string date { get; set; }
public string id_cat { get; set; }
public string id_cat_sub { get; set; }
public string id_user { get; set; }
}我正在使用newtonsoft json
var m_res = JsonConvert.DeserializeObject<YourJsonClass>(YourJsonResponce);
foreach (dynamic numb in m_res.result)
{
string m_name = numb.Name; // it will be "1", "0" or whatever
string h = numb.Value.ToString();
var m_value = JsonConvert.DeserializeObject<Item>(h);
}..。确实有更好的方法,但我希望这会有所帮助(:
https://stackoverflow.com/questions/16060150
复制相似问题