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

  • Home
  • SEARCH
  • 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 4626322
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:24:11+00:00 2026-05-22T03:24:11+00:00

json ={ data: [ { id: 1000, from: { name: Anthony Waema, category: message,

  • 0
json ="{
   "data": [
      {
         "id": "1000",
         "from": {
            "name": "Anthony Waema",
            "category": "message",
            "id": "192"
         },
         "message": "this is the message",
         "updated_time": "2001-05-06T19:34:15+0000",
         "likes": {
            "data": [
               {
                  "id": "100001692250255",
                  "name": "\u00dcnal Turanl\u0131"
               },
               {
                  "id": "100001060078996",
                  "name": "S\u00e9f\u00e2 K\u00e2ql\u00e4Nn"
               }]
              },
         {
         "id": "10150186255753553",
         "from": {
            "name": "another name",
            "category": "message",
            "id": "100001"
         },
         "message": "this is the message",
         "updated_time": "2001-04-06T19:34:15+0000",
         "likes": {
            "data": [
               {
                  "id": "1002345",
                  "name": "\u00dcnal Turanl\u0131"
               },
               {
                  "id": "100234",
                  "name": "S\u00e9f\u00e2 K\u00e2ql\u00e4Nn"
               }]
              }
         }
]
}";

public class Allstatus
    {
        public List<sdata> data { get; set; }
        public scpaging paging { get; set; }
    }

public class sdata
    {

        public string id { get; set; }
        public sfrom from { get; set; }
        public string message { get; set; }
        public string updated_time {get; set;}
        public List<likesdata> likes { get; set; }
    }
   public class likesdata
    {
        public string id{ get; set; }
        public string name{ get; set; }
    }
public class sfrom 
{
      public string name {get; set;}
      public string category {get; set;}
      public string id {get; set;}
}

                JavaScriptSerializer ser = new JavaScriptSerializer();
                page = ser.Deserialize<allstatus>(json);
                foreach (sdata cd in page.data)
                {
                    foreach(likesdata ld in  cd.likes.data)
                    {
                        Console.WriteLine(ld.id+"\t"+ld.name);
                    }

                }

problem:
I need to parse the json and retrieve likes data.
I can access “from” data but not “likes” data.. I get nullreference error when I do this. help needed here.. Thanks.

  • 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-22T03:24:11+00:00Added an answer on May 22, 2026 at 3:24 am

    Edit2:

    Referring https://gist.github.com/973510, its clear from the returned json, that if a particular facebook message doesnt have any likes, then the returned json doesnt contain a property called likes. Hence likes property of sdata object is null. Thats just how the server returns the data.

    There are two ways you can deal with this. Either do a manual check whether likes is null. Or initialize the likes property in the sdata constructor. And initialize the likesdata list in the likesdatacollection constructor.

    Code:

    public class sdata
    {
        // other properties
    
        public likedatacollection likes { get; set; }
    
        public sdata()
        {
            likes = new likedatacollection();
        }
    }
    
    public class likedatacollection
    {
        public List<likesdata> data { get; set; }
    
        public likedatacollection()
        {
            data = new List<likesdata>();
        }
    }
    

    This way, even if fb doesnt return any likes, the constructors will initialize the properties, so they will not be null. You can then check whether likes.data.Count > 0. If yes, then fb returned likes, else fb didnt return likes.

    Edit1:

    From the OP’s comment, its clear that the json is properly formed. Meaning, the json is as retrieved from some server api. Therefore it is the sdata class that is the culprit. Please look at this gist for the full solution.

    The short version. For the simplest case, your c# classes need to follow the exact same structure as your json. As per the json, data has a property called likes. the likes object has a property called data which is an array of objects with properties id and name.

    So your c# class sdata should have a property called likes of type likesdatacollection. This class should have a property data of type List<likesdata>…

    Off topic, people generally seem to prefer Json.Net … so you may want to use that. The reason I use it is because I need it to work in a .Net 2.0 code base …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having difficulty parsing some JSON data returned from my server using jQuery.ajax() To
I'm building a dynamic ExtJS form based on JSON data loaded from an ASP.NET
I have an application which obtains data in JSON format from one of our
Web app. Get data from services in json & xml formats. And from internal
My json data looks like this, {Table : [{accid : 13,accname : Default,accountType :
My Gae application retrieves JSON data from a third party site; given an ID
I need to get json data from an external domain. I used WebRequest to
I've got some JSON data, but it's all on one line. Does anyone know
I need to render JSON data in my JSPs for some AJAX requests. I'd
I am loading JSON data to my page and using appendTo() but I am

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.