【问题标题】:Parse JSON results - C#解析 JSON 结果 - C#
【发布时间】:2011-05-02 16:32:03
【问题描述】:

提前感谢您的帮助!

好的,所以我正在尝试从 API 解析 Json 结果,我绝不是 Json 专家,但返回的数据结果对我来说似乎有点难以掌握(它似乎返回一个'header' 然后是项目...参见原始结果示例)。我一直在尝试使用 Json.NET http://james.newtonking.com/projects/json/help/ 来解析它(这似乎是一个非常棒的工具,只是对它还不够熟悉,或者 Json 就这点而言)。

我正在寻找可能有使用此特定 dll 经验的人或对更好/更简单工具的建议? (我正在尝试解析结果以理想地映射到一个类)。

-谢谢

原始返回结果 (Json)

{
"code": 0,
"message": "Successful",
"partials": {
    "_key": "partial",
    "0": {
        "datetime": "2011-03-08 16:22:51",
        "customer_id": "373263",
        "domain": "xyz.com ** deleted 2011-04-08 18:26:55 UTC**",
        "name": "Joe Customer",
        "phone": "1234567894",
        "email": "joecustomer@test.com",
        "offer": "",
        "pub_id": "",
        "sub_id": "",
        "data1": "",
        "data2": "",
        "data3": "",
        "custom1": "",
        "custom2": "",
        "custom3": "",
        "custom4": "",
        "custom5": "",
        "custom6": "",
        "custom7": "",
        "custom8": "" 
    },
    "1": {
        "datetime": "2011-03-08 16:43:11",
        "customer_id": "373296",
        "domain": "abc.com ** deleted 2011-04-08 18:26:55 UTC**",
        "name": "Jane Customer",
        "phone": "1234567891",
        "email": "janecustomer@test.com",
        "offer": "",
        "pub_id": "",
        "sub_id": "",
        "data1": "",
        "data2": "",
        "data3": "",
        "custom1": "",
        "custom2": "",
        "custom3": "",
        "custom4": "",
        "custom5": "",
        "custom6": "",
        "custom7": "",
        "custom8": "" 
    }
} 

}

我的尝试

        HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();

        if (request.HaveResponse == true)
        {
            Stream responseStream = webResponse.GetResponseStream();
            StreamReader responseReader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
            string responseString = responseReader.ReadToEnd();

            JObject o = JObject.Parse(responseString);

            IList<JToken> results = o["partials"].Children().ToList();

            IList<bbParial> oResults = new List<bbPartial>();

            foreach (JToken t in results)
            {
                if (t.ToString().Contains("partial"))
                {
                    // Do nothing this is the header
                }
                else
                {
                    bbPartial bbp = JsonConvert.DeserializeObject<bbPartial>(t.ToString());
                }
            }
        }

示例类

public class bbpartial
{
    public string _key;
    public string datetime;
    public string customer_id;
    public string domain;
    public string name;
    public string phone;
    public string email;
    public string offer;
    public string pub_id;
    public string sub_id;
    public string data1;
    public string data2;
    public string data3;
    public string custom1;
    public string custom2;
    public string custom3;
    public string custom4;
    public string custom5;
    public string custom6;
    public string custom7;
    public string custom8;
}

【问题讨论】:

  • 你能发布一些你已经尝试过的代码吗?
  • 你能发布一个你想要填充的类的例子吗?
  • 你试过.NET框架中的JavaScriptSerializer吗? msdn.microsoft.com/en-us/library/bb355316.aspx
  • 另请参阅,stackoverflow.com/questions/4523410 - 我为什么要使用 JavaScriptSerializer 以外的任何东西?
  • 感谢守门员,这实际上是一个 winforms 应用程序...对不起,应该在前面提到。

标签: c# json json.net


【解决方案1】:
JObject jsonObj = JObject.Parse(jasonExample);

Customer customerOne = new Customer()
 {
   Name =(string)jsonObj.selectToken("partials[0].name")
 }

这行得通吗?有用吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 2013-06-12
    • 1970-01-01
    • 2013-05-22
    • 2014-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多