Currently I am working on a soundcloud downloader in C#. With the help of the SoundCloud API I get a JSON string of a playlist, which includes a lot of information of the tracks:
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. Is there a way to make this iteration process faster?
You could try using Newtonsoft JSON Deserializer.
For your case you could do something like this:
Create a Track class with needed properties
Apply DeserializeObject
Track jsonObject = JsonConvert.DeserializeObject<Track >(json);
Iterate over jsonObject
Perhaps looping over the properties using JProperty, performs better?
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));
}
Related
I have two auto generated JSON data as shown below. I want to get all the data in Colors Tag.
I Created a JArray of "Colors" and started looping on all data available, but when there is only one color available my code starts giving exceptions as "Colors" is not a JArray instead a JProperty. See sample code below.
What is the ideal way of handling this situation?
{
"UID":1234,
"Colors":["red", "blue", "green"]
}
{
"UID":1234,
"Colors":"green"
}
JObject jsonObject = (JObject)JsonConvert.DeserializeObject(jsonText, settings);
foreach (JObject reg in jsonObject["Colors"]) {
// Write to console.
}
First I would let your schema JSON "Colors" be an array because that can let your JSON be a strong schema.
if you can't modify the schema You can try to use JObject.Parse then use is to judge the type
var jsonObject = JObject.Parse(jsonText);
if (jsonObject["Colors"] is JArray)
{
foreach (JObject reg in jsonObject["Colors"]) {
// Write to console.
}
}
else if(jsonObject["Colors"] is JToken)
{
//jsonObject["Colors"]
}
I am using NewtonSoft to try to parse a jSON array in C# Winform solution. Can anyone help me figure out how to do this?
["yahoo!",["yahoo mail","yahoo finance","yahoo news","yahoo mail login","yahoo sports","yahoo fantasy football","yahoo fantasy","yahoo india","yahoo groups","yahoo search"]]
string json = Helpers.GetGoogleSuggestionKeyword("Yahoo!").ToString();
JArray GoogleSuggesionKeywordResult = JArray.Parse(json);
foreach (JObject item in GoogleSuggesionKeywordResult)
{
// MessageBox.Show(item.ToString());
}
//var json = "[\"yahoo!\",[\"yahoo mail\",\"yahoo finance\",\"yahoo news\",\"yahoo mail login\",\"yahoo sports\",\"yahoo fantasy football\",\"yahoo fantasy\",\"yahoo india\",\"yahoo groups\",\"yahoo search\"]]";
string json = Helpers.GetGoogleSuggestionKeyword("Yahoo!").ToString();
JArray GoogleSuggesionKeywordResult = JArray.Parse(json);
//JArray GoogleSuggesionKeywordResult = JsonConvert.DeserializeObject<JArray>(json);
foreach (JToken item in GoogleSuggesionKeywordResult[1])
{
Console.WriteLine((string)item);
}
i have used JArray & JsonConverter. Which method you preferred you can use it
I have a JSON file I'm reading from text and parsing it into JObject using Newtonsoft.Json.Linq. The JSON file looks like this:
{
"EntityTypeDto":[
{
"EntityType":"Grade",
"Language":"ES"
},
{
"EntityType":"ApplicationType",
"Language":"ES"
},
{
"EntityType":"Borough",
"Language":"ES"
}
]
}
Using the Newtonsoft library, are there any methods I can leverage on JObject to replace the Language property of all the objects to another value? If not what would be another way to do this? This project is a console application in C#, VS 2012, thanks.
You don't need Linq here to achieve what you need , Linq is for consult data, not for modify it. So you can just, eg, a foreach to iterate and modify the elements of the array:
JObject json= JObject.Parse(jsonString);
JArray entityTypeDtos= (JArray)json["EntityTypeDto"];
foreach(var e in entityTypeDtos)
{
if(e["Language"] != null)
e["Language"]="EN";
}
I'm guessing by the Linq tag you would like a Linq approach try this
string json = #"{
'EntityTypeDto':[
{
'EntityType':'Grade',
'Language':'ES'
},
{
'EntityType':'ApplicationType',
'Language':'ES'
},
{
'EntityType':'Borough',
'Language':'ES'
}
]
}";
JObject myjobj = JObject.Parse(json);
JArray EntityType = (JArray)myjobj["EntityTypeDto"];
(from eobj in EntityType
where eobj["Language"]="ES"
select eobj).ForEach(x => x["Language"]="New Value");
I am trying to parse a json file in WP8. For the moment I just need to get a list of topics with some titles each one. Something like:
[
{"topic":"topic1",
"titles":[{"title":"tit1"},
{"title":"tit2"},
{"title":"tit3"}]},
{"topic":"topic1",
"titles":[{"title":"tit1"},
{"title":"tit2"},
{"title":"tit3"}]}
]
My idea is get each topic and save in an array of 2 dimensions. In topic[X][0] would go topic and topic[x][y] the titles...
I have found this topic: Deserializing JSON using JSon.NET with dynamic data
In which is explained a bit how to do it but I am not able to get any of my data because the structure of the json is not similar. Any idea of how to do in this case?
To parse just call:
JArray json = JsonConvert.DeserializeObject(jsonText) as JArray;
For getting the topics just access it normally:
JObject arrayItem = json[0] as JObject;
Getting the topic and it's value:
JValue topic = arrayItem["topic"] as JValue;
string topicValue = topic.Value.ToString();
Getting the titles:
JArray titles = ArrayItem["titles"] as JArray;
And getting their values:
foreach (JObject jo in titles)
{
JValue title = jo["title"] as JValue;
string titleValue = title.Value.ToString();
}
I have a json file that i read to string and then convert to a jobject. I get a JToken from this object by selecting one of its children.
I want to list this JTokens children in a Listbox. I think, to do this i need to convert th eJTOken to a ListItem - how can i do this? If there is a better alternative way then would be interested to hear it!
string filePath = #"C:\output.json";
JObject json = JObject.Parse(System.IO.File.ReadAllText(filePath));
JToken jsonFiles = json["Files"];
jsonFilesListItem = ....
JsonListBox.Items.Add(jsonFilesListItem);
Assuming that json["Files"] contains a simple array of strings, all you have to do is use a foreach loop, as shown in the example below:
string jsonString = #"
{
""Files"": [
""foo.xml"",
""bar.txt"",
""baz.jpg"",
""quux.wav""
]
}";
JObject json = JObject.Parse(jsonString);
JToken jsonFiles = json["Files"];
foreach (string fileName in jsonFiles)
{
JsonListBox.Items.Add(fileName);
}