从动态JSON属性获取值C#

时间:2017-03-04 04:04:48

标签: c# json

我正在研究WCF,我希望明天获得记录列表数组,并且我需要数组键作为日期,这在日期中具有常见的记录,如下所示:

String pathPosts = path + "/posts&debug=all";

基本上,我想提取{ "EventAppGetAllSessionByCustomerIdResult":{ "02/22/2017":[ { "SessionDate":"02/22/2017" } ], "08/27/2016":[ { "SessionDate":"08/27/2016" } ], "Status":{ "Description":"Successfull!", "Status":1 } } } 的值。

2 个答案:

答案 0 :(得分:2)

我假设您要从JSON中提取import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Lab5 extends Lab4 implements ActionListener{ public void init(){ super.init(); order.addActionListener(this); } public void actionPerformed(ActionEvent e) { System.out.println(" At Method action Performed"); String whichButton = e.getActionCommand(); if (whichButton.equals("Place Order")) displayData(); validate(); } public void displayData(){ System.out.println(" At Method Display Data"); String sandwich,drink,side,size; boolean grilled; sandwich = (String) sandwichlist.getSelectedValue(); drink = (String) drinklist.getSelectedValue(); side = (String) sidelist.getSelectedValue(); size = (String) myChoice.getSelectedItem(); grilled = grill.isSelected(); label9.setText(size + grilled + sandwich + side +"with" + drink); } } 属性。我建议使用JObject.Parse()方法。

"SessionDate"

小解释:

在您的情况下,JObject jObject = JObject.Parse(json); var result = (JObject)jObject["EventAppGetAllSessionByCustomerIdResult"]; var dates = new List<string>(); foreach(JProperty prop in result.Properties()) { if (prop.Name != "Status") { var values = jObject["EventAppGetAllSessionByCustomerIdResult"][prop.Name].Values<string>("SessionDate"); dates.AddRange(values); } } 是具有对象数组的属性。每个对象都具有"02/22/2017"属性,该属性具有值。因此,以下行将从所有对象的"SessionDate"中提取值:

"SessionDate"

var values = jObject["EventAppGetAllSessionByCustomerIdResult"][prop.Name].Values<string>("SessionDate"); 代表单个属性的所有日期。在您的情况下,它可以来自values或来自"02/22/2017"

"08/27/2016"将是dates值的列表。当然,您必须自己处理可能的异常。

答案 1 :(得分:0)

我不确定你想要的是什么,但尝试将其作为输出对象:

public class Session
{
    public string SessionDate { get; set; }
}
public class Status
{
    public string Description { get; set; }
    public int Code { get; set; }
}
public class EventAppGetAllSessionByCustomerIdResult
{
    public KeyValuePair<string, Session[]>[] EventAppGetAllSessionByCustomerId { get; set; }

    public Status Status { get; set; }
}