Parsing JSON object containing an array with Windows Phone 7

前端 未结 3 1220
终归单人心
终归单人心 2021-01-14 17:36

Ok, I\'m having some difficult with this.

My JSON is like

{ \"names\" : [ {\"name\":\"bla\"} , {\"name\":\"bla2\"} ] }

I was trying

3条回答
  •  生来不讨喜
    2021-01-14 18:07

    Using Json.Net (which supports Windows Phone)

    string json = @"{ ""names"" : [ {""name"":""bla""} , {""name"":""bla2""} ] }";
    
    var dict = (JObject)JsonConvert.DeserializeObject(json);
    foreach (var obj in dict["names"])
    {
        Console.WriteLine(obj["name"]);
    }
    

    Or if you want to use it in a type-safe way

    var dict = JsonConvert.DeserializeObject(json);
    foreach (var obj in dict.names)
    {
        Console.WriteLine(obj.name);
    }
    
    
    public class RootClass
    {
        public MyName[] names { get; set; }
    }
    
    public class MyName
    {
        public string name { get; set; }
    }
    

提交回复
热议问题