关于c#:反序列化Json String .net

Deserialize Json String .net

我想将下面的json字符串反序列化为3个不同的DataGrid。一种用于结果,一种用于提款,一种用于存款。有人有这样做的好方法吗?任何帮助将非常感激。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
{
 "results": [
    {
     "id":"51142254",
     "tp_id":"!XP4D49X0CD123628",
     "firstname":"Owner",
     "lastname":"Operator",
     "email":"",
     "phone":"",
     "enrolled":"1",
     "balance": 247.54,
     "fleet":"Test Express",
     "deposits": [
        {
         "id":"184022380",
         "date":"2016-02-17",
         "amount":"200.00",
         "transID":"135246",
         "memo":"Scheduled Deposit",
         "status":"Cleared"
        },
        {
         "id":"184022383",
         "date":"2016-02-25",
         "amount":"200.00",
         "transID":"246357",
         "memo":"Scheduled Deposit",
         "status":"Cleared"
        },
        {
         "id":"184022386",
         "date":"2016-03-02",
         "amount":"200.00",
         "transID":"975468",
         "memo":"Scheduled Deposit",
         "status":"Cleared"
        }
      ],
     "withdrawals": [
        {
         "id":"184026949",
         "date":"2016-03-09",
         "amount":"352.46",
         "transID":"395920",
         "memo":"Invoice\
\
\
\
100234"
,
         "status":"Cleared"
        }
      ]
    },
    {
     "id":"51142326",
     "tp_id":"!XP4D49X7CD123612",
     "firstname":"Owner",
     "lastname":"Operator",
     "email":"",
     "phone":"",
     "enrolled":"1",
     "balance": 0,
     "fleet":"Test\
\
Express"
,
     "deposits": [],
     "withdrawals": []
    }
  ]
}

当我转到json2chsarp.com时,这就是为我的班级生成的内容。我假设存款和提款部分未显示,因为此处未列出子节点。如何正确完成此操作?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Result
{
    public string id { get; set; }
    public string tp_id { get; set; }
    public string firstname { get; set; }
    public string lastname { get; set; }
    public string email { get; set; }
    public string phone { get; set; }
    public string enrolled { get; set; }
    public double balance { get; set; }
    public string fleet { get; set; }
    public List<object> deposits { get; set; }
    public List<object> withdrawals { get; set; }
}

public class RootObject
{
    public List<Result> results { get; set; }
}


Newtonsoft JSON库是处理JSON对象的绝佳选择。通过将动态功能与JObject.Parse结合使用,您可以使用(其中rawJson是JSON字符串)进行解析后,从JSON对象中提取数据:

1
2
    dynamic jsonBody = JObject.Parse(rawJson);
    dynamic results = jsonBody.results;

遍历结果数组,可以获取要插入到数据网格中所需的所有字段和数据:

1
2
3
4
5
6
7
8
9
10
11
12
    foreach(dynamic curResult in results)
    {
      JArray deposits = (JArray)curResult.deposits;
      JArray withdrawals = (JArray)curResult.withdrawals;

      //To get column names and values for your first data grid, iterate over the properties within curResult record
      JObject header = (JObject)curResult;
      var headerObjects = header.Properties().
          Find(x =>
              !x.Name.Equals("deposits") ||
              !x.Name.Equals("withdrawals"));
    }

您可以在每个结果中使用deposits数组和提现数组中的记录来填充这些数据网格。

这只是该库允许您遍历JSON文档的众多方式之一。我希望这会有所帮助。


首先,我建议创建一个直接映射到Json字符串中给定的对象和属性的对象(及其后续对象)。

然后可以使用JsonConvert.DeserializeObject<T>("your json string here");其中T是您的新对象类型,可以将其转换回新对象的实例。

别忘了在类上使用Newtonsoft.Json

转到Newtonsoft的网站以查看如何执行此操作/如何工作。

我也会看看另一个有用的答案,也有类似的问题。

希望这会有所帮助。


您必须创建一个Results类,并定义JSON具有的每个属性。而且,您还必须定义一个用于存款和提款的类,并将结果列表和列表放在结果类中。然后您可以使用此

1
var results = new JavaScriptSerializer().Deserialize<List<results>>();

希望这会有所帮助

此后,您可以使用您的对象填充您的数据网格


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class Deposite
{
    public int id { get; set; }
    public date date { get; set; }
    public double amount { get; set; }
    public int transID { get; set; }
    public string Memo { get; set; }
    public string status { get; set; }
}

public class Withdrawal
{
    public int id { get; set; }
    public date date { get; set; }
    public double amount { get; set; }
    public int transID { get; set; }
    public string Memo { get; set; }
    public string status { get; set; }
}

public class results
{
    public int id { get; set; }
    public int tp_id{ get; set; }
    public string firstname { get; set; }
    public string lastname { get; set; }
    public string email { get; set; }
    public string phone { get; set; }
    public string enrolled { get; set; }
    public string balance { get; set; }
    public string fleet { get; set; }
    public Deposite[] deposite { get; set; }
    public Withdrawal[] withdrawal { get; set; }
}

public class opt
{
    public results[] response { get; set; }
}

using System.Web.Script.Serialization;

JavaScriptSerializer objJS = new JavaScriptSerializer();
opt objopt  = new opt();
objopt  = objJS .Deserialize<opt >("Your String");