我已经有这个使用Silverlight的System.JSON但规格的回答得到了改变,现在我需要做的是在.net 3.5反序列化JSON使用JSON.NET
这里是我得到
的JSON{"SearchResults":[{"PageCount":"1"},
{"SEARCHVAL":"Result","CATEGORY":"Category1","X":"1","Y":"2"},
{"SEARCHVAL":"AnotherResult","CATEGORY":"Category1","X":"2","Y":"2"}]}
,并使用System.JSON组装的解决方案是
var resultList = ((JsonArray)searchResults["SearchResults"])
.OfType<JsonObject>()
.Where(o => o.ContainsKey("SEARCHVAL"))
.Select(o => new SearchResult() {
SearchValue = o["SEARCHVALUE"],
Category = o["CATEGORY"].
X = o["X"],
Y = o["Y"]
}).ToList();
我想大部分代码类似于/一模一样的,但我不知道有关的containsKey在JSON.net对口。我认为这是Contains()方法,但我不确定如何使用它,以便我可以获得SEARCHVAL的X和Y.
UPDATE:
所以这里是我的代码来获得JSON流和解析:
...
Uri uri = new Uri(url);
WebClient client = new WebClient();
ParseJSON(client.OpenRead(uri));
}
private void ParseJSON(Stream stream)
{
if (stream == null)
return;
StreamReader reader = new StreamReader(stream);
JObject searchResult = JObject.Parse(reader.ReadLine());
string x= searchResult["SearchResults"][0]["SEARCHVAL"]["X"].ToString();
string y= searchResult["SearchResults"][0]["SEARCHVAL"]["Y"].ToString();
// use data
...
和我得到的string lat = searchresult...
空例外。任何线索我在哪里使用JSON.NET出错?
您已链接到JavaScriptSerializer。事实上,如果它不限于Silverlight,那么使用System.JSON会很好。 – Bahamut