gpt4 book ai didi

c# - 解析 JSON 结果 - C#

转载 作者:行者123 更新时间:2023-11-30 18:05:07 25 4
gpt4 key购买 nike

在此先感谢您的帮助!

我正在尝试从 API 解析 Json 结果,但数据的返回结果对我来说似乎有点难以理解。

  • 它似乎返回一个“标题”,然后返回项目(...参见原始结果示例)。
  • 我尝试使用 Json.NET 解析它

我对 Json.NET 和 Json 都不熟悉。

  • 如何解析结果并将其映射到类?
  • 使用 JSON.Net 或其他工具。

谢谢

原始返回结果(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": "", ... , "data3": "",
"custom1": "", ..., "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": "", ... , "data3": "",
"custom1": "", ..., "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;
}

最佳答案

JObject jsonObj = JObject.Parse(jasonExample);

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

那有用吗?有用吗?

关于c# - 解析 JSON 结果 - C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5859724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com