Parse and Extract Attributes from JSON

2019-03-02 07:30发布

I have a large JSON file similar to this:

{
   "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":[

         ]
      }      
   ]
}

I want to extract from the a list of all the url attribute values from the standard_resolution attribute of all the images within the JSON. How can it be done?

3条回答
男人必须洒脱
2楼-- · 2019-03-02 07:55

You can use the Linq features of JSON.net, along with the select token method to get at the data you are looking for:

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();
查看更多
成全新的幸福
3楼-- · 2019-03-02 07:56

Add reference

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

Make a string variable using 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\":[]}    ]}";

Create a method inside the class in which conversion takes place.

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

Add these classes below the class having conversion

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

Place this code where you need conversion

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

Use foreach to loop across the variable dataObj.

Example Parsing JSON object containing an array with Windows Phone 7

查看更多
再贱就再见
4楼-- · 2019-03-02 08:04

I have used the JSON class in System.Web.Helpers Namespace (.Net 4.0) previously and it works well for me. You are able to refer to arrays dynamically. It should be used similarly to this:

dynamic myJson = Json.Decode(myJsonString);
foreach (var url in myJson.data.images.standard_resolution){
//DO SOMETHING
}
查看更多
登录 后发表回答