在c#中解码(JSON) HttpWebResponse
本文关键字:JSON HttpWebResponse 解码 | 更新日期: 2023-09-27 17:49:38
下面是我的代码:
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
var vrati = Newtonsoft.Json.JsonConvert.DeserializeObjectAsync(responseString);
log.Text = vrati["f"][0];
我使用Newtonsoft. json。JsonConvert,我不知道怎么做。
JSON代码像
{"a":13,"o":215,"f":["g","i"]}
我想得到["f"][0] ..我样本里的"g"请帮帮我
如果您想要动态,快速和脏的东西,您可以:
var dynJson = JsonConvert.DeserializeObject<dynamic>("{'"a'":13,'"o'":215,'"f'":['"g'",'"i'"]}");
Console.WriteLine(dynJson.a);
Console.WriteLine(dynJson.o);
foreach(var something in dynJson.f){
Console.WriteLine(something);
}
如果你想要一些类型,你可以创建一个对象匹配你的Json和反序列化:
void Main()
{
var dynJson = JsonConvert.DeserializeObject<MyThing>("{'"a'":13,'"o'":215,'"f'":['"g'",'"i'"]}");
Console.WriteLine(dynJson.a);
Console.WriteLine(dynJson.o);
foreach(var something in dynJson.f){
Console.WriteLine(something);
}
}
public class MyThing {
public int a {get;set;}
public int o {get;set;}
public List<string> f {get;set;}
}
试试这个,使用动态
string responseString = "{'"a'":13,'"o'":215,'"f'":['"g'",'"i'"]}";
dynamic vrati = JObject.Parse(responseString);
log.Text = vrati["f"][0];
您可以将其解析为动态对象:
string data = "{'"a'":13,'"o'":215,'"f'":['"g'",'"i'"]}";
dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject(data);
string result = obj.f[0].Value;