Newtonsoft.Json解析数组的小例子

{
    "status": 0,
    "message": "query ok",
    "result": {
        "address": "北京市海淀区彩和坊路海淀西大街74号",
        "address_component": {
            "province": "北京市",
            "city": "北京市",
            "district": "海淀区",
            "street": "彩和坊路",
            "street_number": "海淀西大街74号"
        },
        "pois": [
            {
                "id": "3629720141162880123",
                "title": "中国技术交易大厦",
                "address": "北京市海淀区北四环西路66号",
                "category": "房产小区;商务楼宇",
                "location": {
                    "lat": "39.984122",
                    "lng": "116.307484"
                },
                "_distance": "3.6"
            },
            {
                "id": "2845372667492951071",
                "title": "中国技术交易大厦A座",
                "address": "北京市海淀区北四环西路66号",
                "category": "房产小区;商务楼宇",
                "location": {
                    "lat": "39.984273",
                    "lng": "116.307577"
                },
                "_distance": "15.2"
            },
            {
                "id": "12925244666643621769",
                "title": "中国技术交易大厦B座",
                "address": "北京市海淀区北四环西路66号",
                "category": "房产小区;商务楼宇",
                "location": {
                    "lat": "39.983902",
                    "lng": "116.307588"
                },
                "_distance": "29.3"
            },
            {
                "id": "7472400256925846331",
                "title": "中国化工博物馆",
                "address": "海淀区北四环西路62号中国化工集团大厦3楼(海淀桥西)",
                "category": "文化场馆;博物馆",
                "location": {
                    "lat": "39.984582",
                    "lng": "116.308877"
                },
                "_distance": "127.5"
            },
            {
                "id": "16243165360295251323",
                "title": "贝塔咖啡",
                "address": "北京市海淀区北四环西路66号中关村第三极大厦1层西北侧",
                "category": "娱乐休闲;咖啡厅",
                "location": {
                    "lat": "39.984391",
                    "lng": "116.307380"
                },
                "_distance": "28.0"
            },
            {
                "id": "7246616758286733108",
                "title": "基督教堂",
                "address": "北京市海淀区彩和坊路9号",
                "category": "旅游景点;教堂",
                "location": {
                    "lat": "39.983146",
                    "lng": "116.307507"
                },
                "_distance": "112.2"
            },
            {
                "id": "8627298709465036679",
                "title": "北京三木和商店",
                "address": "北京市海淀区北四环西路66号中关村文化商厦三层D-006-009单元",
                "category": "购物;综合商场",
                "location": {
                    "lat": "39.984093",
                    "lng": "116.307983"
                },
                "_distance": "42.6"
            },
            {
                "id": "12020256857742021617",
                "title": "图书城昊海楼",
                "address": "北京市海淀区海淀西街36号",
                "category": "房产小区;住宅区;住宅小区",
                "location": {
                    "lat": "39.984400",
                    "lng": "116.306794"
                },
                "_distance": "65.4"
            },
            {
                "id": "10394251724976454044",
                "title": "北京点点酷东东商贸中心海淀分部",
                "address": "北京市海淀区北四环西路66号中关村文化商厦2B001",
                "category": "购物;综合商场",
                "location": {
                    "lat": "39.984093",
                    "lng": "116.307983"
                },
                "_distance": "42.6"
            },
            {
                "id": "16427755502147943355",
                "title": "北京资源燕园宾馆",
                "address": "北京市海淀区颐和园路1号",
                "category": "酒店宾馆;星级酒店",
                "location": {
                    "lat": "39.986712",
                    "lng": "116.305822"
                },
                "_distance": "318.3"
            }
        ]
    }
}




HttpWebRequest request = (HttpWebRequest)result.AsyncState;
HttpWebResponse response = (HttpWebResponse)(request.EndGetResponse(result));
stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream, false);
string apiText = reader.ReadToEnd();
JObject jsonObj = null;
try
{
    jsonObj = JObject.Parse(apiText);
    if (jsonObj.Count == 1 || (int)(jsonObj["status"]) != 0) this.isError = true;
    else
    {
        string provinceName = (string)jsonObj["result"]["address_component"]["province"];
        string cityName = this.cityName_s = (string)jsonObj["result"]["address_component"]["city"];
        string districtName = (string)jsonObj["result"]["address_component"]["district"];
        string street = (string)jsonObj["result"]["address_component"]["street"];
        /*下面是解析JArray的部分*/
        JArray jlist = JArray.Parse(jsonObj["result"]["pois"].ToString()); //将pois部分视为一个JObject,JArray解析这个JObject的字符串
        LocationItem locationitem = null;      //存储附近的某个地点的信息
        locations = new List<LocationItem>();  //附近位置的列表
        for(int i = 0; i < jlist.Count ; ++i)  //遍历JArray
        {
            locationitem = new LocationItem();
            JObject tempo = JObject.Parse(jlist[i].ToString());
            locationitem.id = tempo["id"].ToString();
            locationitem.title = tempo["title"].ToString();
            locationitem._distance = tempo["_distance"].ToString();
            locationitem.address = tempo["address"].ToString();
            locationitem.category = tempo["category"].ToString();
            locationitem.location.lat = tempo["location"]["lat"].ToString();
            locationitem.location.lng = tempo["location"]["lng"].ToString();
            locations.Add(locationitem);
        }
    }
}
catch (Exception)
{
    isError = true;
}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值