[英]How to read JSON response in c#
I am using thirparty service to give me coordinates, below is the response I want to read this using c# .net in some kind of object so that I can use the information but confused how to achieve this.. 我正在使用第三方服务给我坐标,以下是我想在某种对象中使用c#.net读取此响应,以便我可以使用这些信息,但对如何实现此操作感到困惑。
{"found": 1, "bounds": [[52.45401, -1.96211], [52.45401, -1.96211]], "features": [{"id": 65140,"centroid": {"type":"POINT","coordinates":[52.45401, -1.96211]},"bounds": [[52.45401, -1.96211], [52.45401, -1.96211]],"properties": {"name": "B17 0SL"},"type": "Feature"}], "type": "FeatureCollection", "crs": {"type": "EPSG", "properties": {"code": 4326, "coordinate_order": [0, 1]}}}
Thanks 谢谢
have a look at Newtonsoft.Json its a package that will deserialize the Json into a class for you. 看一下Newtonsoft.Json的软件包,它将为您反序列化Json到一个类。
but you will need to create the class structure you want to use. 但是您将需要创建要使用的类结构。
Use a json parser like DataContractJsonSerializer or JavaScriptSerializer 使用JSON分析器,例如DataContractJsonSerializer或JavaScriptSerializer
For your case for ex., using Json.Net & dynamic keyword, you can write 对于例,使用Json.Net和dynamic关键字,您可以编写
dynamic jObj = JsonConvert.DeserializeObject(jsonstr);
Console.WriteLine(jObj.found);
Console.WriteLine(jObj.features[0].bounds[0][0]);
You can use JsonTextReader
. 您可以使用
JsonTextReader
。 The following code segment may be useful, if you are not using JSON.NET 如果您不使用JSON.NET,以下代码段可能会很有用
jsonString = {"found": 1, "bounds": [[52.45401, -1.96211], [52.45401, -1.96211]], "features": [{"id": 65140,"centroid": {"type":"POINT","coordinates":[52.45401, -1.96211]},"bounds": [[52.45401, -1.96211], [52.45401, -1.96211]],"properties": {"name": "B17 0SL"},"type": "Feature"}], "type": "FeatureCollection", "crs": {"type": "EPSG", "properties": {"code": 4326, "coordinate_order": [0, 1]}}};
JsonTextReader reader = new JsonTextReader(new StringReader(jsonString));
while (reader.Read())
{
if (reader.Value != null)
{
// Read Json values here
// reader.Path -> Gives you the key part.
// reader.Value.ToString() -> Gives you the value part
}
}
You can read the JSON like this: 您可以像这样读取JSON:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("URL");
JArray array = new JArray();
using (var twitpicResponse = (HttpWebResponse)request.GetResponse())
{
using (var reader = new StreamReader(twitpicResponse.GetResponseStream()))
{
JavaScriptSerializer js = new JavaScriptSerializer();
var objText = reader.ReadToEnd();
JObject joResponse = JObject.Parse(objText);
JObject result = (JObject)joResponse["result"];
array = (JArray)result["Detail"];
string statu = array[0]["dlrStat"].ToString();
}
}
您可以使用JSON.NET
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.