我知道有一些关于 Newtonsoft 的帖子,所以希望这不是重复...我正在尝试将 Kazaa 的 API 返回的 JSON 数据转换为某种不错的对象
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 可能是一个更好的选择?想法/链接?
以下是 JSON 返回数据的示例:
{
"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": "\u76e7\u5de7\u97f3"
},
{
"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,下载文件,并在七行代码中完成所有操作!我喜欢它。
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();
这篇文章获得了如此多的点击率,我认为包含评论中讨论的“使用”位可能会有所帮助。
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"]);
}
您可以使用 C# dynamic
键入以使事情变得更容易。这种技术还使重构更简单,因为它不依赖于魔法字符串。
JSON
下面的 JSON 字符串是来自 HTTP API 调用的简单响应,它定义了两个属性:Id
和 Name
.
{"Id": 1, "Name": "biofractal"}
C#
使用 JsonConvert.DeserializeObject<dynamic>()
将此字符串反序列化为动态类型,然后以通常的方式访问其属性。
dynamic results = JsonConvert.DeserializeObject<dynamic>(json);
var id = results.Id;
var name= results.Name;
如果您指定 results
的类型变量为 dynamic
, 而不是使用 var
关键字,则属性值将正确反序列化,例如Id
到 int
而不是 JValue
(感谢 Gfoley83 在下面的评论)。
注意:Newtonsoft 程序集的 NuGet 链接是 http://nuget.org/packages/newtonsoft.json .
包:您还可以使用 nuget live 安装程序添加包,打开您的项目只需执行 browse package然后安装它install, unistall, update ,它只会添加到您的项目下的 Dependencies/NuGet
我是一名优秀的程序员,十分优秀!