C# 将JSON反序列化到相对类
我不熟悉JSON和Web API。我已经阅读了帮助书籍,制作了一个简单的控制台应用程序,可以获得URL响应,即JSON,并使用NewtonSoft解析它。我无法从JSON访问C# 将JSON反序列化到相对类,c#,asp.net,json,json.net,C#,Asp.net,Json,Json.net,我不熟悉JSON和Web API。我已经阅读了帮助书籍,制作了一个简单的控制台应用程序,可以获得URL响应,即JSON,并使用NewtonSoft解析它。我无法从JSON访问VoiceIAQStats部分数据。我需要解析方面的帮助: 我的代码是: async static void GetRequest(string url) { using (HttpClient client = new HttpClient()) { using (HttpResponseM
VoiceIAQStats
部分数据。我需要解析方面的帮助:
我的代码是:
async static void GetRequest(string url)
{
using (HttpClient client = new HttpClient())
{
using (HttpResponseMessage response = await client.GetAsync(url))// .GetAsync(url))
{
using (HttpContent content = response.Content)
{
string mycontent = await content.ReadAsStringAsync();
//Console.WriteLine(mycontent);
JArray a = JArray.Parse(mycontent);
Console.WriteLine("Total Count is " + a.Count());
Console.WriteLine("Data is " + a.ToString());
}
}
}
}
JSON结果如下所示:
[{“id”:“faisal”,“operation”:“UPDATE”,“VoiceIAQStats”:{“id”:9,“esdId”:9,“esdName”:“faisal”}]
您正在解析一个数组adn,因此所有数据都保存在数组的第一个元素中。您应该访问数组的第一项,然后访问属性,如下所示:
var firstElement = a[0];
var voiceIaqStats = firstElement["VoiceIAQStats"];
//Or in 1 line:
var voiceIaqStats = a[0]["VoiceIAQStats"];
将结果作为对象将使您的生活更加轻松。我想试试这样的东西。(我自己还没有测试过)
私有结构响应数据
{
公共列表结果{get;set;}
}
私有结构数据
{
公共字符串id{get;set;}
公共字符串操作{get;set;}
public VoiceIA VoiceIAQStats{get;set;}
}
私有结构语音
{
[JsonProperty(“id”)]
公共长id{get;set;}
[JsonProperty(“esdId”)]
公共长esdId{get;set;}
[JsonProperty(“esdName”)]
公共字符串esdName{get;set;}
}
异步静态void GetRequest(字符串url)
{
使用(HttpClient=new HttpClient())
{
使用(httpresponsemessageresponse=await client.GetAsync(url))/.GetAsync(url))
{
使用(HttpContent=response.content)
{
var mycontent=content.ReadAsStringAsync();
ResponseData queryResponse=JsonConvert.DeserializeObject(mycontent.Result);
字符串id=queryResponse.results[0].id;
int count=queryResponse.results.count;
字符串操作=queryResponse.results[0]。操作;
VoiceIA voiceObj=queryResponse.results[0]。VoiceIAQStats;
long idOfVoice=voiceObj.id;
long esdId=voiceObj.esdId;
字符串esdName=voiceObj.esdName;
////控制台。写线(mycontent);
//JArray a=JArray.Parse(mycontent);
//Console.WriteLine(“总计数为”+a.Count());
//Console.WriteLine(“数据是”+a.ToString());
}
}
}
}
private struct ResponseData
{
public List<Data> results { get; set; }
}
private struct Data
{
public string id { get; set; }
public string operation { get; set; }
public VoiceIA VoiceIAQStats { get; set; }
}
private struct VoiceIA
{
[JsonProperty("id")]
public long id { get; set; }
[JsonProperty("esdId")]
public long esdId { get; set; }
[JsonProperty("esdName")]
public string esdName { get; set; }
}
async static void GetRequest(string url)
{
using (HttpClient client = new HttpClient())
{
using (HttpResponseMessage response = await client.GetAsync(url))// .GetAsync(url))
{
using (HttpContent content = response.Content)
{
var mycontent = content.ReadAsStringAsync();
ResponseData queryResponse = JsonConvert.DeserializeObject<ResponseData>(mycontent.Result);
string id = queryResponse.results[0].id;
int count = queryResponse.results.Count;
string operation = queryResponse.results[0].operation;
VoiceIA voiceObj = queryResponse.results[0].VoiceIAQStats;
long idOfVoice = voiceObj.id;
long esdId = voiceObj.esdId;
string esdName = voiceObj.esdName;
//// Console.WriteLine(mycontent);
//JArray a = JArray.Parse(mycontent);
//Console.WriteLine("Total Count is " + a.Count());
//Console.WriteLine("Data is " + a.ToString());
}
}
}
}