如果我在浏览器中查看url
获取:
{"status":{"message":"the daily limit of 30000 credits for demo has been exceeded. Please use an application specific account. Do not use the demo account for your application.","value":18}}
因此JSON有两个元素“status”和“value”
按照以下示例。当我DeserializeObject(text)
data.Count返回3
但wc.DownloadString(url)
返回:
"{\"status\":{\"message\":\"the daily limit of 30000 credits for demo has been exceeded. Please use an application specific account. Do not use the demo account for your application.\",\"value\":18}}"
在DeserializeObject(json)
data.Count之后返回null。
我试试:
json = json.Replace(@"\", "");
但是“\”并没有删除,而且我也不想删除原始JSON上的任何“\”。
public class HomeController : Controller
{
static string url = "http://api.geonames.org/findNearByWeatherJSON?lat=-68,1&lng=10,2&username=demo";
public ActionResult Index()
{
var text = @"[{'Name':'AAA','Age':'22','Job':'PPP'},
{'Name':'BBB','Age':'25','Job':'QQQ'},
{'Name':'CCC','Age':'38','Job':'RRR'}]";
using (WebClient wc = new WebClient())
{
var json = wc.DownloadString(url);
dynamic data = Newtonsoft.Json.JsonConvert.DeserializeObject(text);
// data.Count = 3
data = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
// data.Count = null
}
return View();
}
}
注意:即使重复的问题显示为什么调试器中的显示与文本显示不同,问题是DeserializeObject
无法正常工作