检索json对象中属性的值,这也是jsonobject的属性

时间:2012-05-19 13:53:52

标签: c# json

我有jsonobject的问题,我想从jsonobject(位置)检索属性“name”,它也是jsonobject(朋友)的属性。这是我的动态变量朋友

   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"}

可以检索位置的属性名称

1 个答案:

答案 0 :(得分:1)

        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();
        }