I am trying to access all the categories in the first level within a Foursquare response:-
{
"meta": {
"code": 200
},
"response": {
"categories": [{
"id": "4d4b7104d754a06370d81259",
"name": "Arts & Entertainment",
"pluralName": "Arts & Entertainment",
"shortName": "Arts & Entertainment",
"icon": {
"prefix": "https:\/\/foursquare.com\/img\/categories\/arts_entertainment\/default_",
"sizes": [32, 44, 64, 88, 256],
"name": ".png"
},
"categories": [{
"id": "4bf58dd8d48988d1e1931735"
using JSON.NET:-
JObject o = JObject.Parse(FoursquareObject.GetCategories());
IList<string> categories = o.SelectToken("categories[0]").Select(s => (string)s).ToList();
Where FoursquareObject.GetCategories() returns the response as a string. I also tried:-
JArray categories = (JArray)o["categories"];
var categories = (string) o["response[0].categories"];
...and numerous variations of, just to see the response in the variable and always get 'Object reference' or 'cannot be {null}' errors. I know I'm close, but for the life of me can't work out how to get at the 'categories' part of the response...
Can anyone point me in the right direction?
Help is appreciated. ;)
UPDATE:
Thanks to the answers from L.B and Meklarian, I added this code (and variants of):-
dynamic four = JsonConvert.DeserializeObject(FoursquareObject.GetCategories());
foreach (var cat in four)
{
context.Response.Write(cat.response.categories.id);
}
But regardless of what I try in the Write(), I always get:-
'Newtonsoft.Json.Linq.JProperty' does not contain a definition for
'response'
I tried loads of combinations, no-luck. I checked the output from the JSON file, I get pure JSON response as a string. Just a note, that categories can exist in categories, hence why the JSON seems to look broken. I assure you it's not. I'm completely stuck!
There are multiple ways to parse this; but in the style you've initially presented, you should try accessing it with [] indexers.
Note that your data has a root object with two properties, meta and response. Assuming response can't be null, you can access categories from it directly like this:
var root = JObject.Parse(/* your json string here */);
var categories = root["response"]["categories"];
var firstCategory = categories[0];
Note that you can use strings matching property names to descend into nested levels, and integers to index into arrays in scope.
Here is the rest of a sample program that can parse the json snippet you've provided.
using System.Windows.Forms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
namespace _4sqCatResponse
{
class Program
{
[STAThread]
static void Main(string[] args)
{
OpenFileDialog dlg = new OpenFileDialog();
if(dlg.ShowDialog() != DialogResult.OK){return;}
string json = System.IO.File.ReadAllText(dlg.FileName);
var root = JObject.Parse(json);
var categories = root["response"]["categories"];
var firstCategory = categories[0];
Console.WriteLine("id: {0}", firstCategory["id"]);
Console.WriteLine("name: {0}", firstCategory["name"]);
Console.WriteLine("pluralName: {0}", firstCategory["pluralName"]);
Console.WriteLine("shortName: {0}", firstCategory["shortName"]);
var icon = firstCategory["icon"];
Console.WriteLine("icon.prefix: {0}", icon["prefix"]);
Console.WriteLine("icon.sizes[0]: {0}", icon["sizes"][0]);
Console.WriteLine("icon.name: {0}", icon["name"]);
Console.ReadKey();
}
}
}
Also I'm a little confused by your json sample; I think you may have overlaid part of your sample or cut something out as you have categories nested in categories. If there is indeed a 2nd level categories inside your categories element and that's what you want, you can access it with this:
var categories2 = root["response"]["categories"][0]["categories"][0];
Console.WriteLine("inner categories id: {0}", categories2["id"]);
Here is the json source I used to test, copied from yours but with closing } and ] marks where needed to make it parse.
{
"meta": {
"code": 200
},
"response": {
"categories": [{
"id": "4d4b7104d754a06370d81259",
"name": "Arts & Entertainment",
"pluralName": "Arts & Entertainment",
"shortName": "Arts & Entertainment",
"icon": {
"prefix": "https:\/\/foursquare.com\/img\/categories\/arts_entertainment\/default_",
"sizes": [32, 44, 64, 88, 256],
"name": ".png"
},
"categories": [{
"id": "4bf58dd8d48988d1e1931735"
}]
}]
}
}
I think this should work
dynamic four2 = JsonConvert.DeserializeObject(FoursquareObject.GetCategories());
foreach(var cat in four2.response.categories)
{
Console.WriteLine(cat.id + " " + cat.name + " " + cat.icon.prefix);
}
Use http://json2csharp.com/ to generate C# classes, use RootObject and then access the individual venues
Related
I am making rest call and receving following JSON response:
{
"issues": [{
"id": "250271",
"self": "KeyUrl1",
"key": "Key-8622",
"fields": {
"attachment": [{
"self": "AttachmentUrl1",
"id": "106198",
"filename": "export.htm"
}
],
"customfield_11041": "Test"
}
},
{
"id": "250272",
"self": "KeyUrl2",
"key": "Key-8621",
"fields": {
"attachment": [{
"self": "AttachmentUrl2",
"id": "106199",
"filename": "lmn.htm"
}
],
"customfield_11041": "Test"
}
},
]
}
I parsed it using NewtonSoft Json to JObject.
var jObject = JObject.Parse(response);
Further I am trying to filter such records where either attachment is missing or none of the attachments contain filename like "export".
Following is the code I have written, ideally it should result in just 1 record in the records object, however its returning both the objects.
var issues = jObject["issues"] as JArray;
var records = issues.Where(x => !x["fields"]["attachment"].Any() || !x["fields"]["attachment"].Any(y => y["filename"].Contains("export")));
Need help to figure out whats going wrong.
Here's fiddle link - https://dotnetfiddle.net/AVyIHr
The problem is that you're calling Contains("export") on the result of y["filename"], which isn't a string - it's a JToken. You need to convert to a string first, to use the form of Contains that you're expecting.
Additionally, you can get rid of the first condition - an issue with no attachments doesn't have any attachments with "export" filename anyway.
That leaves this:
var records = issues
.Where(x => !x["fields"]["attachment"].Any(y => ((string) y["filename"]).Contains("export")))
.ToList();
You may well find it's simpler to deserialize to a class, however - that will reduce the risk of typos and the risk of this sort of conversion error. If you deserialized to a List<Issue> you'd have a condition of:
x => !x.Fields.Attachments.Any(y => y.Filename.Contains("export"))
... which I think is rather cleaner.
var records = issues.Where(x => !x["fields"]["attachment"].Any() || !x["fields"]["attachment"].Any(y => y["filename"].ToString().Contains("export"))).ToList();
Add .ToString() will resolve the issue.
I am converting XML to JSON.
Input:
<emp
id="17377"/>
<CustomerList>
<Customer
id="67149"/>
<Customer id="64260"/>
</CustomerList>
OutPut:
"emp": {
"id": "17377"
},
"CustomerList": {
"Customer": [
{
"id": "67149"
},
{
"id": "64260"
}
]
}
But I need the below output. But I can not remove <Customer from <CustomerList> in the input. Also Please note that I need accept dynamic name
of array input. But always i want to remove the inner property name to be removed. in this example its Customer.But I may get MarkList->Mark then I need to remove remove Mark, etc.,:
"emp": {
"id": "17377"
},
"CustomerList": [
{
"id": "67149"
},
{
"id": "64260"
}
]
Is this possible please.
I use below code to convert XML to Json:
var xml = new XmlDocument();
xml.XmlResolver = null;
xml.LoadXml(richTextBox1.Text);
var jsonText = JsonConvert.SerializeXmlNode(xml,Newtonsoft.Json.Formatting.Indented);
Note:
One solution would be find the char "[" and remove before "[" and after "{".
This is not possible, as it is simply trying to change to JSON scheme in which it was orignally built.
what you can do, is use JObject to change the value of customer to feet your needs:
JObject rss = JObject.Parse(json);
JObject customers = rss.SelectToken("CustomerList");
customers ["Customer"] = newValue;
This is the snippet, modify this in your code to feet your needs.
How can i acces the first item id?
using (var http = new HttpClient())
{
var res = JArray.Parse(await http.GetStringAsync("http://api.champion.gg/champion/Gragas?api_key=????").ConfigureAwait(false));
^^^^^^ // Also tried with JObject instead of JArray, both don't work
var champion = (Uri.EscapeUriString(res[0]["items"][0]["mostGames"][0]["items"][0]["id"].ToString()));
Console.WriteLine(champion); // ^ [0] here because the JSON starts with an [
}
Example JSON result (made it smaller because the original JSON is over 21500 characters, made sure its valid with https://jsonlint.com, here is the original JSON response: https://hastebin.com/sacikozano.json)
[{
"key": "Gragas",
"role": "Jungle",
"overallPosition": {
"change": 1,
"position": 13
},
"items": {
"mostGames": {
"items": [{
"id": 1402,
"name": "Enchantment: Runic Echoes"
},
{
"id": 3158,
"name": "Ionian Boots of Lucidity"
},
{
"id": 3025,
"name": "Iceborn Gauntlet"
},
{
"id": 3065,
"name": "Spirit Visage"
},
{
"id": 3742,
"name": "Dead Man's Plate"
},
{
"id": 3026,
"name": "Guardian Angel"
}
],
"winPercent": 50.45,
"games": 300
}
}
}]
With JArray i get the following error: Accessed JObject values with invalid key value: 0. Object property name expected.
With JObject i get the following error: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.
Thanks in advance, i hope i explained it well
It should be:
var champion = (Uri.EscapeUriString(res[0]["items"]["mostGames"]["items"][0]["id"].ToString()));
The outermost "items" property has a single object as its value, not an array, so [0] is not needed in ["items"][0]. Similarly "mostGames" has a single object value so the [0] is not needed in ["mostGames"][0].
Sample fiddle.
Note that if "items" is sometimes an array of objects, but sometimes is a single object instead of an array of one object, you can introduce the following extension method:
public static class JsonExtensions
{
public static IEnumerable<JToken> AsArray(this JToken item)
{
if (item is JArray)
return (JArray)item;
return new[] { item };
}
}
And do:
var champion = (Uri.EscapeUriString(res[0]["items"].AsArray().First()["mostGames"]["items"][0]["id"].ToString()));
I'm getting the following Json format string as a result from a Facebook graph search request :
{
"data": [
{
"name": "Event A",
"start_time": "2013-11-08T19:00:00+0200",
"end_time": "2013-11-10T00:00:00+0200",
"timezone": "Europe/Bucharest",
"id": "232252355126"
},
{
"name": "Event B",
"start_time": "2013-11-08T13:00:00+0200",
"end_time": "2013-11-09T16:00:00+0200",
"timezone": "Europe/Bucharest",
"location": "Bucharest",
"id": "414334343426"
},
{
"name": "Event C",
"start_time": "2013-10-30T18:30:00+0200",
"timezone": "Europe/Bucharest",
"location": "Bucharest",
"id": "44315995273"
}
],
"paging": {
"previous": "https://graph.facebook.com/search?limit=3&type=event&q=Bucharest&since=1383930000&__paging_token=22251255126",
"next": "https://graph.facebook.com/search?limit=3&type=event&q=Bucharest&until=1383150600&__paging_token=44115995273"
}
}
I'm encountering some errors while trying to retrieve data from this JSON. I've tried with
dynamic jsonData = await facebookClient.GetTaskAsync(string.Format("https://graph.facebook.com/search?q={0}&type=event&limit={1}&offset={2}", locationKeyword, limit, offset));
dynamic result = JsonConvert.DeserializeObject(jsonData.ToString());
Some answers direct me to use JavaScriptSerializer but I don't have the namespace for that class, as I'm using API for developing Windows 8 Apps.
I can't manage how to get the events as somehow from data object.
I tried accessing the values in the immediate windows in VS as result.data but it's not working.
I search on how to make this but most answers seem to say to create a class in which the json data will fit.
Can't I achieve this with dynamic? (something like result.data.name, result.paging.previous etc)
I have done this exact thing before, except I converted into an XML, My Example:
(1 - JavaScript) var finalStr = JSON.stringify(facebookString)
(2 - ASP.NET) JsonConvert.DeserializeXmlNode("{\"root\":" + received_json + "}","root");
I managed at last to access the members...
To access name for example, or start_time, I did the following :
dynamic jsonData = await facebookClient.GetTaskAsync(string.Format("https://graph.facebook.com/search?q={0}&type=event&limit={1}&offset={2}", locationKeyword, limit, offset));
var result = JObject.Parse(jsonData.ToString());
var array = new JArray(result["data"]);
var a = array[0];
string name = (string) a.SelectToken("name");
var date = (DateTime?) a.SelectToken("start_time");
There might be better implementations but this one worked in my case. I posted it, in case it might be of help to others seeing this post.
Best wishes.
Lets say i have the following JSON
{
"data": [
{
"from": {
"name": "aaa bbb",
},
"actions": [
{
"name": "Comment",
"link": "http://...
},
{
"name": "Like",
"link": "http://.."
}
],
},
And i have
JSONObject wallData = helper.Get("/me/feed");
if (wallData != null)
{
var data = wallData.Dictionary["data"];
List<JSONObject> wallPosts = data.Array.ToList<JSONObject>();
}
foreach (Facebook.JSONObject wallItem in wallPosts)
{ ... }
Which stores me whole feed into wallData and 'data' object into wallPosts.
So then i can access the wallItem.Dictionary["from"].Dictionary["name"], and i get "aaa bbb".
But i can't get inside the actions array
The wallItem.Dictionary["actions"].Dictionary["name"] doesn't work.
Any idea
You need to do something like wallItem.Dictionary["actions"][0].Dictionary["name"] because "actions" is an array.
On a different note...its neater if u directly into a class...like this
var jSerializer = new JavaScriptSerializer();
var jsonObject = jSerializer.Deserialize<DataObject>(json);
The DataObject will be a class which emulates ur JSON data in a strongly typed class. Depending on the size of ur Json you will not have to use a lot of strings in your code.