[英]c# iterate through json
Currently I am working on a soundcloud downloader in C#. 目前,我正在使用C#开发Soundcloud下载器。 With the help of the SoundCloud API I get a JSON string of a playlist, which includes a lot of information of the tracks: 借助SoundCloud API,我获得了播放列表的JSON字符串,其中包括很多曲目信息:
http://pastebin.com/HfrjqyJE http://pastebin.com/HfrjqyJE
I tried it with: 我尝试了:
JObject results = JObject.Parse(e.Result);
foreach (var result in results["tracks"])
{
string streamUrl = (string)result["stream_url"];
string title = (string)result["title"];
}
it worked but it needs about 20 secs to iterate through a playlist with only 2 tracks. 它可以正常工作,但只需要20秒钟即可遍历只有2首曲目的播放列表。 Is there a way to make this iteration process faster? 有没有办法使此迭代过程更快?
Perhaps looping over the properties using JProperty, performs better? 也许使用JProperty遍历属性会更好?
string json = "{a: 10, b: 'aaaaaa', c: 1502}";
JObject parsedJson = JObject.Parse(json);
foreach (JProperty property in parsedJson.Properties())
{
Console.WriteLine(string.Format("Name: [{0}], Value: [{1}].", property.Name, property.Value));
}
You could try using Newtonsoft JSON Deserializer . 您可以尝试使用Newtonsoft JSON Deserializer 。
For your case you could do something like this: 对于您的情况,您可以执行以下操作:
Create a Track class with needed properties 创建具有所需属性的Track类
Apply DeserializeObject 套用DeserializeObject
Track jsonObject = JsonConvert.DeserializeObject<Track >(json);
Iterate over jsonObject 遍历jsonObject
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.