最近,我得到了一个约会,我需要使用它的REST API从云端下载一些数据,并将该字符串的值存储在对象或某些变量中。下载完成后,我现在需要做的就是解析数据。C#中JSON格式字符串的值(来自REST API)
到现在为止我做了这样的代码:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("some link");
request.ContentType = "application/json; charset=utf-8";
request.Method = "GET";
request.Headers.Add("Carriots.apiKey", "key");
using (WebResponse response = request.GetResponse())
{
using(Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream);
JObject json = JObject.Parse(reader.ReadToEnd());
Console.WriteLine(json.ToString());
}
}
这里是输出:
{
"total_documents": 3,
"result": [
{
"_id": "...",
"protocol": "v1",
"checksum": "",
"_t": "str",
"at": 1444134377,
"device": "-just a device-",
"data": {
"field1": "123",
"field2": "1536"
},
"id_developer": "....",
"created_at": 1444134377,
"owner": "-someUser-"
}
]
}
我知道有很多解决方案,在互联网上,但他们没有一个做我需要的。好吧,我找到了一些东西,但是它在每一行上迭代并以这种方式检查值,但在我的情况下,我可以拥有数千个这样的输出。
有什么办法可以做到这一点(我的意思是解析)使用某种内置函数或唯一的解决方案是迭代或写一些正则表达式?
你读取JSON在C#或JS? – stackoverfloweth
反序列化就是你要找的东西! –
试试Linq to Json http://www.newtonsoft.com/json/help/html/LINQtoJSON.htm – Matteo