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
Related
I need to add some more data into existing JSON
eg:
{
"OrderId":"abc",
"products":["a","b","c","etc"]
}
how to add more into products
The approach would be similar to used in answer to your previous question, but you will need to convert element to JArray:
var x = #"{
'OrderId':'abc',
'products':['a','b','c','etc']
}";
var jObj = JObject.Parse(x);
((JArray)jObj["products"]).Add("new");
Try this:
var jObject = JObject.Parse(json);
var jArray = jObject["products"] as JArray;
jArray?.Add("new_product");
I have a JSON that looks like this:
{
"Identifier1":"TextOfIdentifier1",
"Identifier2":"TextOfIdentifier2",
"Identifier3":"TextOfIdentifier3",
...
}
I know how to deseralize a JSON into a custom object, I followed what says here, but all identifiers appear in the same JSON tag...
How can I get all identifiers inside the JSON?
The solution is like this in my case:
using (StreamReader r = new StreamReader(path))
{
string json = r.ReadToEnd();
JObject jsonLines = JObject.Parse(json);
foreach (var token in jsonLines)
{
dtos.Add(new TokenDto { HalId = token.Key, SourceText = token.Value.ToString() });
}
}
You can traverse JSON (similar to XDocument):
var json = "{\"Identifier1\":\"TextOfIdentifier1\",\"Identifier2\":\"TextOfIdentifier2\",\"Identifier3\":\"TextOfIdentifier3\"}";
foreach (var token in JObject.Parse(json).Children())
Console.WriteLine(token.Path);
Result:
Identifier1
Identifier2
Identifier3
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));
}
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);
}