Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7570113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:20:21+00:00 2026-05-30T15:20:21+00:00

I am trying to access all the categories in the first level within a

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:20:23+00:00Added an answer on May 30, 2026 at 3:20 pm

    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"
                    }]
                }]
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to access all my select tags with a specific name. I've tried
I'm trying to write a subroutine in access 2003 that removes all quote characters
I am new to Nokogiri and XPath, and I am trying to access all
I am trying to access my local resources file in my code-behind. I did
Am trying to parse the RSS feed from www.ted.com/talks/rss, I can access all normal
I am trying to access permissions of all folders(not hidden) in a site for
I'm trying to authenticate access to all resources on my server via HTTP Basic
I am trying to access all files in a directory and do some thing
Im trying to access a web service from a remote computer. I managed to
Im trying to access the Magento customer session in another part of my website.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.