我正在尋找一個簡單的示例,從一個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