弘帝企业智能建站系统交流平台

 找回密码
 立即注册
查看: 3785|回复: 1

【C#】json字符串如何读值

[复制链接]
发表于 2017-4-9 17:15:49 | 显示全部楼层 |阅读模式
利用Newtonsoft.Json反序列解析。
参考案例:HD_Maps.aspx获得IP当地地理经纬坐标
json:
{"address":"CN|\u5b89\u5fbd|\u829c\u6e56|None|CHINANET|0|0","content":{"address":"\u5b89\u5fbd\u7701\u829c\u6e56\u5e02","address_detail":{"city":"\u829c\u6e56\u5e02","city_code":129,"district":"","province":"\u5b89\u5fbd\u7701","street":"","street_number":""},"point":{"x":"118.38410842","y":"31.36601979"}},"status":0}
根据json字符串值建立规则类
public class JsonParser
{
    public Content content { get; set; }
}
public class Content
{
    public Point point { get; set; }
}
public class Point
{
    public string x { get; set; }
    public string y { get; set; }
}
如果不会设计C#类,可以把JSON字符串复制到网址:http://json2csharp.chahuo.com/,自动生成JSON
主程序:
//JsonParser为自定义类
JsonParser jp = (JsonParser)JsonConvert.DeserializeObject<JsonParser>(jsonStr);
// 根据自定义类获取指定值
xy = jp.content.point.x +","+jp.content.point.y;


回复

使用道具 举报

 楼主| 发表于 2019-5-10 06:49:42 | 显示全部楼层
public static Json StringToJson<Json>(string StringJson) where Json : class
{
Json json = JsonConvert.DeserializeObject<Json>(StringJson);
return json;
}
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|弘帝企业智能建站系统 ( 皖ICP备07503252号 )

GMT+8, 2024-4-20 01:44 , Processed in 0.054745 second(s), 15 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表