WebRequest request = WebRequest.Create("url" + result + "url");
string json;
var response = request.GetResponse();
request.ContentType = "application/json; charset=utf-8";
using (var streamr = new StreamReader(response.GetResponseStream()))
{
json = streamr.ReadToEnd();
MessageBox.Show(json);
}
我有这段代码可以获得以下示例字符串
[{"Type":1,"Country":"CA","Channel":"","Code":"1EZ","Start":"2014-10-24T00:00:00","End":"2015-10-23T00:00:00"},{"Type":2,"Country":"","Channel":"","Code":"UAD","Start":"2014-10-24T00:00:00","End":"2017-10-23T00:00:00"},{"Type":2,"Country":"","Channel":"","Code":"TPQ","Start":"2014-10-24T00:00:00","End":"2017-10-23T00:00:00"},{"Type":3,"Country":"","Channel":"","Code":"SVC_PRIORITY","Start":"2014-10-24T00:00:00","End":"2017-10-23T00:00:00"}]
但我想只获得Start&结束值 - 是否有可以执行此操作的JSON解释器?
答案 0 :(得分:4)
让你入门的东西。使用JSON.NET将json反序列化为类:
var jsonStr = "[{\"Type\":1,\"Country\":\"CA\",\"Channel\":\"\",\"Code\":\"1EZ\",\"Start\":\"2014 - 10 - 24T00: 00:00\",\"End\":\"2015 - 10 - 23T00: 00:00\"},{\"Type\":2,\"Country\":\"\",\"Channel\":\"\",\"Code\":\"UAD\",\"Start\":\"2014 - 10 - 24T00: 00:00\",\"End\":\"2017 - 10 - 23T00: 00:00\"},{\"Type\":2,\"Country\":\"\",\"Channel\":\"\",\"Code\":\"TPQ\",\"Start\":\"2014 - 10 - 24T00: 00:00\",\"End\":\"2017 - 10 - 23T00: 00:00\"},{\"Type\":3,\"Country\":\"\",\"Channel\":\"\",\"Code\":\"SVC_PRIORITY\",\"Start\":\"2014 - 10 - 24T00: 00:00\",\"End\":\"2017 - 10 - 23T00: 00:00\"}]";
var myObjectList = JsonConvert.DeserializeObject<List< MyObject>>(jsonStr);
你的班级例子:
public class MyObject
{
public string Type { get; set; }
public string Country { get; set; }
public string Channel { get; set; }
public string Code { get; set; }
public string Start { get; set; }
public string End { get; set; }
}
答案 1 :(得分:4)
使用http://json2csharp.com/从json生成类:
public class RootObject
{
public int Type { get; set; }
public string Country { get; set; }
public string Channel { get; set; }
public string Code { get; set; }
public string Start { get; set; }
public string End { get; set; }
}
然后从Nuget添加JSON.NET。
将json反序列化为生成的对象:
List<RootObject> o = JsonConvert.DeserializeObject<List<RootObject>>(json);
获得有趣的属性。