我正在寻找一个简单的示例,从一个url调用一个feed,然后循环遍历数据在C#中拉出值。.NET处理JSON供稿
我设法得到饲料数据到像这样的字符串变量。我看了看newtonsoft.Json DLL,但找不到一个简单的例子,将数据拉出来。数据并不复杂,我已将其添加到底部。
所以基本_feedData现在包含我的JSON数据我不知何故想将它转换成一个JSON对象,然后foreach它拉出的值。
static void Main(string[] args)
{
string _feedData = GetJSONFeed();
}
public static string GetJSONFeed()
{
string formattedUri = "http://www.myJsonFeed.com/blah.json";
HttpWebRequest webRequest = GetWebRequest(formattedUri);
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
string jsonResponse = string.Empty;
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
jsonResponse = sr.ReadToEnd();
}
return jsonResponse;
}
private static HttpWebRequest GetWebRequest(string formattedUri)
{
// Create the request’s URI.
Uri serviceUri = new Uri(formattedUri, UriKind.Absolute);
// Return the HttpWebRequest.
return (HttpWebRequest)System.Net.WebRequest.Create(serviceUri);
}
我的JSON数据是这样的:
[
{
"id": "9448",
"title": "title title title",
"fulltext": "main body text",
"url": "http://www.flikr.co.uk?id=23432"
},
{
"id": "9448",
"title": "title title title",
"fulltext": "main body text",
"url": "http://www.flikr.co.uk?id=23432"
}
]
感谢您的帮助。 Rob
这个帖子有你需要的一切:http://stackoverflow.com/questions/1212344/parse-json-in-c – Nix 2010-10-30 15:37:50