2014-02-11 57 views
0

屬性我有一個類似的大型JSON文件:分析和提取從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":[ 

     ] 
     }  
    ] 
} 

我想從所有url屬性值的列表,從內所有圖像的standard_resolution屬性提取JSON。如何做呢?

+4

使用[JSON.Net](http://james.newtonking.com/json) –

+0

可以使用'JToken變量= JToken.Parse(數據);''然後將Varible'成爲一個'數組' –

回答

1

我已經使用了JSON類System.Web.Helpers命名空間(.Net 4.0)以前,它適用於我。您可以動態地引用數組。應該同樣使用這樣的:

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

可以使用JSON.net的LINQ的特點,與選擇標記方法以及在數據讓你在尋找:

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

使用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\":[]} ]}"; 

創建其中轉換髮生在類中添加一個方法參考

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

做一個字符串變量。

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

,你需要轉換

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

使用的foreach跨越可變dataObj循環添加這些類的有級轉化

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

將下面這段代碼。

Parsing JSON object containing an array with Windows Phone 7