从JSON解析和提取属性
我有一个类似于这个的大型JSON文件:
{ "data":[ { "attribution":null, "tags":[ "thenight2" ], "type":"image", "images":{ "standard_resolution":{ "url":"https://distilleryimage3.s3.amazonaws.com/59d6984092a211e392db12e25f465f4f_8.jpg", "width":640, "height":640 } } }, { "attribution":null, "tags":[ "thenight2" ], "type":"image", "images":{ "low_resolution":{ "url":"https://distilleryimage1.s3.amazonaws.com/c179b34a91ff11e3b99c0aa73e1070c5_6.jpg", "width":306, "height":306 }, "thumbnail":{ "url":"https://distilleryimage1.s3.amazonaws.com/c179b34a91ff11e3b99c0aa73e1070c5_5.jpg", "width":150, "height":150 }, "standard_resolution":{ "url":"https://distilleryimage1.s3.amazonaws.com/c179b34a91ff11e3b99c0aa73e1070c5_8.jpg", "width":640, "height":640 } }, "users_in_photo":[ ] } ] }
我想从JSON中所有图像的standard_resolution
属性中提取所有url
属性值的列表。 怎么做到呢?
我以前在System.Web.Helpers命名空间(.Net 4.0)中使用过JSON类,它对我很有用。 您可以动态引用数组。 它应该与此类似地使用:
dynamic myJson = Json.Decode(myJsonString); foreach (var url in myJson.data.images.standard_resolution){ //DO SOMETHING }
您可以使用JSON.net的Linqfunction以及select token方法来获取您要查找的数据:
String fileContents = System.IO.File.ReadAllText("Z:\temp\test.json"); Newtonsoft.Json.Linq.JObject obj = Newtonsoft.Json.Linq.JObject.Parse(fileContents); IList urls = obj["data"].Select(m => (string)m.SelectToken("images.standard_resolution.url")).ToList();
添加参考
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":"https://distilleryimage3.s3.amazonaws.com/59d6984092a211e392db12e25f465f4f_8.jpg","+ ""width":640,"height":640}}},{"attribution":null,"tags":["thenight2"],"type":"image","images":{"low_resolution":{"+ ""url":"https://distilleryimage1.s3.amazonaws.com/c179b34a91ff11e3b99c0aa73e1070c5_6.jpg","width":306,"height":306},"thumbnail":{"url":"https://distilleryimage1.s3.amazonaws.com/c179b34a91ff11e3b99c0aa73e1070c5_5.jpg","+ ""width":150,"height":150},"standard_resolution":{ "url":"https://distilleryimage1.s3.amazonaws.com/c179b34a91ff11e3b99c0aa73e1070c5_8.jpg","width":640,"height":640"+ "}},"users_in_photo":[]} ]}";
在发生转换的类中创建一个方法。
public static T Deserialize(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>(json);
使用foreach循环遍历变量dataObj。
示例使用Windows Phone 7解析包含数组的JSON对象
上述就是C#学习教程:从JSON解析和提取属性分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1020207.html