有没有简单的方法在c#下面解析JSOn
{"type":"text","totalprice":"0.0045","totalgsm":"1","remaincredit":"44.92293","messages": [ {"status":"1","messageid":"234011120530636881","gsm":"923122699633"} ]}
如果有多个结果.
跟着这些步骤:
使用json2csharp.com将您的JSON转换为C#;
创建一个类文件并将上面生成的代码放在那里;
Newtonsoft.Json
使用Nuget Package Manager 将库添加到项目中;
使用以下代码转换从您的服务收到的JSON:
RootObject r = JsonConvert.DeserializeObject(json);
(随意重命名RootObject
为更有意义的东西.其他类应保持不变.)
您可以安全地使用内置JavaScriptSerializer
而无需引用其他第三方库:
var ser = new System.Web.Script.Serialization.JavaScriptSerializer(); ser.DeserializeObject(json);
我找到了一种无需使用任何外部API即可获取的方法
using (var w = new WebClient()) { var json_data = string.Empty; string url = "YOUR URL"; // attempt to download JSON data as a string try { json_data = w.DownloadString(url); JavaScriptSerializer jsSerializer = new JavaScriptSerializer(); var result = jsSerializer.DeserializeObject(json_data); Dictionaryobj2 = new Dictionary (); obj2=(Dictionary )(result); string val=obj2["KEYNAME"].ToString(); } catch (Exception) { } // if string with JSON data is not empty, deserialize it to class and return its instance }