从JSON解析和提取属性

时间:2014-02-11 03:52:32

标签: c# .net json parsing

我有一个类似于此的大型JSON文件:

{
   "data":[
      {
         "attribution":null,
         "tags":[
            "thenight2"
         ],
         "type":"image",
         "images":{
            "standard_resolution":{
               "url":"http://distilleryimage3.s3.amazonaws.com/59d6984092a211e392db12e25f465f4f_8.jpg",
               "width":640,
               "height":640
            }
         }
      },
      {
         "attribution":null,
         "tags":[
            "thenight2"
         ],
         "type":"image",
         "images":{
            "low_resolution":{
               "url":"http://distilleryimage1.s3.amazonaws.com/c179b34a91ff11e3b99c0aa73e1070c5_6.jpg",
               "width":306,
               "height":306
            },
            "thumbnail":{
               "url":"http://distilleryimage1.s3.amazonaws.com/c179b34a91ff11e3b99c0aa73e1070c5_5.jpg",
               "width":150,
               "height":150
            },
            "standard_resolution":{
               "url":"http://distilleryimage1.s3.amazonaws.com/c179b34a91ff11e3b99c0aa73e1070c5_8.jpg",
               "width":640,
               "height":640
            }
         },
         "users_in_photo":[

         ]
      }      
   ]
}

我想从JSON中所有图片的url属性中的所有standard_resolution属性值列表中提取。怎么办呢?

3 个答案:

答案 0 :(得分:1)

您可以使用JSON.net的Linq功能以及select token方法来获取您要查找的数据:

String fileContents = System.IO.File.ReadAllText("Z:\\temp\\test.json");
Newtonsoft.Json.Linq.JObject obj = Newtonsoft.Json.Linq.JObject.Parse(fileContents);
IList<string> urls = obj["data"].Select(m => (string)m.SelectToken("images.standard_resolution.url")).ToList();

答案 1 :(得分:1)

我之前在System.Web.Helpers命名空间(.Net 4.0)中使用过JSON类,它对我来说效果很好。您可以动态引用数组。它应该与此类似地使用:

dynamic myJson = Json.Decode(myJsonString);
foreach (var url in myJson.data.images.standard_resolution){
//DO SOMETHING
}

答案 2 :(得分:0)

添加参考

using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;

使用JSON

创建一个字符串变量
string json = "{\"data\":[{\"attribution\":null,\"tags\":[\"thenight2\"],\"type\":\"image\",\"images\":{\"standard_resolution\":{\"url\":\"http://distilleryimage3.s3.amazonaws.com/59d6984092a211e392db12e25f465f4f_8.jpg\","+

               "\"width\":640,\"height\":640}}},{\"attribution\":null,\"tags\":[\"thenight2\"],\"type\":\"image\",\"images\":{\"low_resolution\":{"+
              "\"url\":\"http://distilleryimage1.s3.amazonaws.com/c179b34a91ff11e3b99c0aa73e1070c5_6.jpg\",\"width\":306,\"height\":306},\"thumbnail\":{\"url\":\"http://distilleryimage1.s3.amazonaws.com/c179b34a91ff11e3b99c0aa73e1070c5_5.jpg\","+
               "\"width\":150,\"height\":150},\"standard_resolution\":{ \"url\":\"http://distilleryimage1.s3.amazonaws.com/c179b34a91ff11e3b99c0aa73e1070c5_8.jpg\",\"width\":640,\"height\":640"+
            "}},\"users_in_photo\":[]}    ]}";

在进行转换的类中创建一个方法。

public static T Deserialize<T>(string json) where T : new()
        {
            using (MemoryStream memoryStream = new MemoryStream(Encoding.Unicode.GetBytes(json)))
            {
                var serializer = new DataContractJsonSerializer(typeof(T));
                return (T)serializer.ReadObject(memoryStream);
            }
        }

在具有转化的类下面添加这些类

public class Data
    {
        public Users[] users { get; set; }
    }

    public class Users
    {
        public Image image { get; set; }
    }

    public class Image
    {
        public StandardUrl standardUrl { get; set; }
    }

    public class StandardUrl
    {
        public string url { get; set; }
    }

将此代码放在需要转换的位置

var dataObj = Deserialize<List<Data>>(json);

使用foreach循环遍历变量dataObj。

示例Parsing JSON object containing an array with Windows Phone 7