检索 jsonObject 中的属性值,这也是 JSON 对象的优点

本文关键字:JSON 对象 jsonObject 属性 检索 | 更新日期: 2023-09-27 18:34:37

>我对jsonobject有问题,我想从jsonobject(location(中检索属性"name",它也是jsonobject(Friend(的属性。 这是我的动态变量朋友

   friend = {"id":"100001867845514","name":"ucef nahs","location": {"id":"100245266683893","name":"settat, Casablanca, Morocco"}, "picture":"http://profile.ak.fbcdn.net/hprofile-ak-snc4/49453_100001867845514_620239062_q.jpg"}

可以检索位置的属性名称

检索 jsonObject 中的属性值,这也是 JSON 对象的优点

        var jss = new JavaScriptSerializer();
        var ob = jss.Deserialize<Dictionary<string, object>>(
                "{'"id'":'"100001867845514'",'"name'":'"ucef nahs'",'"location'": {'"id'":'"100245266683893'",'"name'":'"settat, Casablanca, Morocco'"}, '"picture'":'"http://profile.ak.fbcdn.net/hprofile-ak-snc4/49453_100001867845514_620239062_q.jpg'"}");
        var location = ob.FirstOrDefault(friend => friend.Key == "location").Value as IDictionary<string, object>;
        if (location != null)
        {
            var locationName = location.FirstOrDefault(elem => elem.Key == "name").Value;
            Console.WriteLine(locationName);
            Console.ReadLine();
        }