問題描述
我知道有一些關于 Newtonsoft 的帖子,所以希望這不是重復...我正在嘗試將 Kazaa 的 API 返回的 JSON 數據轉換為某種不錯的對象
I know there are a few posts about Newtonsoft so hopefully this isn't exactly a repeat...I'm trying to convert JSON data returned by Kazaa's API into a nice object of some kind
WebClient client = new WebClient();
Stream stream = client.OpenRead("http://api.kazaa.com/api/v1/search.json?q=muse&type=Album");
StreamReader reader = new StreamReader(stream);
List<string> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(reader.Read().ToString());
foreach (string item in list)
{
Console.WriteLine(item);
}
//Console.WriteLine(reader.ReadLine());
stream.Close();
那條 JsonConvert 線只是我最近嘗試的一條……我不太明白,希望通過詢問你們來消除一些步法.我最初試圖將它轉換成字典或其他東西......實際上,我只需要在那里獲取幾個值,所以從文檔來看,也許 Newtonsoft 的 LINQ to JSON 可能是一個更好的選擇?想法/鏈接?
That JsonConvert line is just the most recent one I was trying...I'm not quite getting it and was hoping to eliminate some footwork by asking you guys. I was originally trying to convert it into a Dictionary or something...and actually, I just need to snag a couple of values in there so judging by the documentation, maybe Newtonsoft's LINQ to JSON might be a better choice? Thoughts/Links?
以下是 JSON 返回數據的示例:
Here is an example of the JSON return data:
{
"page": 1,
"total_pages": 8,
"total_entries": 74,
"q": "muse",
"albums": [
{
"name": "Muse",
"permalink": "Muse",
"cover_image_url": "http://image.kazaa.com/images/69/01672812 1569/Yaron_Herman_Trio/Muse/Yaron_Herman_Trio-Muse_1.jpg",
"id": 93098,
"artist_name": "Yaron Herman Trio"
},
{
"name": "Muse",
"permalink": "Muse",
"cover_image_url": "htt p://image.kazaa.com/images/54/888880301154/Candy_Lo/Muse/Candy_Lo-Muse_1.jpg",
"i d": 102702,
"artist_name": "u76e7u5de7u97f3"
},
{
"name": "Absolution",
"permalink": " Absolution",
"cover_image_url": "http://image.kazaa.com/images/65/093624873365/Mus e/Absolution/Muse-Absolution_1.jpg",
"id": 48896,
"artist_name": "Muse"
},
{
"name": "Ab solution",
"permalink": "Absolution-2",
"cover_image_url": "http://image.kazaa.com/i mages/20/825646911820/Muse/Absolution/Muse-Absolution_1.jpg",
"id": 118573,
"artist _name": "Muse"
},
{
"name": "Black Holes And Revelations",
"permalink": "Black-Holes-An d-Revelations",
"cover_image_url": "http://image.kazaa.com/images/66/093624428466/ Muse/Black_Holes_And_Revelations/Muse-Black_Holes_And_Revelations_1.jpg",
"id": 48813,
"artist_name": "Muse"
},
{
"name": "Black Holes And Revelations",
"permalink": "Bla ck-Holes-And-Revelations-2",
"cover_image_url": "http://image.kazaa.com/images/86/ 825646911486/Muse/Black_Holes_And_Revelations/Muse-Black_Holes_And_Revelations_1 .jpg",
"id": 118543,
"artist_name": "Muse"
},
{
"name": "Origin Of Symmetry",
"permalink": "Origin-Of-Symmetry",
"cover_image_url": "http://image.kazaa.com/images/29/825646 912629/Muse/Origin_Of_Symmetry/Muse-Origin_Of_Symmetry_1.jpg",
"id": 120491,
"artis t_name": "Muse"
},
{
"name": "Showbiz",
"permalink": "Showbiz",
"cover_image_url": "http: //image.kazaa.com/images/68/825646182268/Muse/Showbiz/Muse-Showbiz_1.jpg",
"id": 60444,
"artist_name": "Muse"
},
{
"name": "Showbiz",
"permalink": "Showbiz-2",
"cover_imag e_url": "http://image.kazaa.com/images/50/825646912650/Muse/Showbiz/Muse-Showbiz_ 1.jpg",
"id": 118545,
"artist_name": "Muse"
},
{
"name": "The Resistance",
"permalink": "T he-Resistance",
"cover_image_url": "http://image.kazaa.com/images/36/825646864836/ Muse/The_Resistance/Muse-The_Resistance_1.jpg",
"id": 121171,
"artist_name": "Muse"
}
],
"per_page": 10
}
<小時>
我做了更多閱讀,發現 Newtonsoft 的 LINQ to JSON 正是我想要的……使用 WebClient、Stream、StreamReader 和 Newtonsoft……我可以點擊 Kazaa 獲取 JSON 數據,提取 URL,下載文件,并在七行代碼中完成所有操作!我喜歡它.
I did some more reading and found Newtonsoft's LINQ to JSON is exactly what I wanted...using WebClient, Stream, StreamReader, and Newtonsoft...I can hit Kazaa for JSON data, extract a URL, download the file, and do it all in like seven lines of code! I love it.
WebClient client = new WebClient();
Stream stream = client.OpenRead("http://api.kazaa.com/api/v1/search.json?q=muse&type=Album");
StreamReader reader = new StreamReader(stream);
Newtonsoft.Json.Linq.JObject jObject = Newtonsoft.Json.Linq.JObject.Parse(reader.ReadLine());
// Instead of WriteLine, 2 or 3 lines of code here using WebClient to download the file
Console.WriteLine((string)jObject["albums"][0]["cover_image_url"]);
stream.Close();
<小時>
這篇文章獲得了如此多的點擊率,我認為包含評論中討論的使用"位可能會有所幫助.
This post gets so many hits I thought it might be helpful to include the "using" bits that are discussed in the comments.
using(var client = new WebClient())
using(var stream = client.OpenRead("http://api.kazaa.com/api/v1/search.json?q=muse&type=Album"))
using (var reader = new StreamReader(stream))
{
var jObject = Newtonsoft.Json.Linq.JObject.Parse(reader.ReadLine());
Console.WriteLine((string) jObject["albums"][0]["cover_image_url"]);
}
推薦答案
如果你只需要從 JSON 對象中獲取一些項目,我會使用 Json.NET 的 LINQ to JSON JObject
類.例如:
If you just need to get a few items from the JSON object, I would use Json.NET's LINQ to JSON JObject
class. For example:
JToken token = JObject.Parse(stringFullOfJson);
int page = (int)token.SelectToken("page");
int totalPages = (int)token.SelectToken("total_pages");
我喜歡這種方法,因為您不需要完全反序列化 JSON 對象.這對于有時會因缺少對象屬性而讓您感到驚訝的 API 非常有用,例如 Twitter.
I like this approach because you don't need to fully deserialize the JSON object. This comes in handy with APIs that can sometimes surprise you with missing object properties, like Twitter.
文檔:序列化和反序列化JSON 與 Json.NET 和 LINQ to JSON with Json.NET
這篇關于使用 Newtonsoft 將 JSON 反序列化為 .NET 對象(或者可能是 LINQ to JSON?)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!