Search code examples
c#jsonwindows-phonejson.net

why do not fetch the ID field from json c#?


I am trying to get the ID from json using the library newtonsoft but the ID is null, while the other fields are correct(with content). The class is:

public class JsonRequestMapping
{
    private String status;
    private String count;
    private String pages;
    private List<PointOfInterest> posts = new List<PointOfInterest>();

    public String Status
    {
        get { return status; }
        set { status = value; }
    }

    public String Count
    {
        get { return count; }
        set { count = value; }
    }

    public String Pages
    {
        get { return pages; }
        set { pages = value; }
    }

    public List<PointOfInterest> Posts
    {
        get { return posts; }
        set { posts = value; }
    }
}

public class PointOfInterest
{

    private string ID;
    private string post_title;
    private string post_content;
    private string post_modified;
    private string featuredimage = null;
    private CustomFields custom_fields;
    private List<string> localPhotosUrl = new List<string>();
    private string latitude = null;
    private string longitude = null;
}

The json that i get is

{"respond":1,"paging":{"stillmore":0,"perpage":"150","callpage":1,"next":2,"previous":0,"pages":1,"result":"103"},"message":"","result":[{"ID":"5712","post_title":"Fabriano","guid":"http:\/\/adriatic-route.com\/webgis\/?post_type=listing&#038;p=5712","post_content":"Even back in the 14th century, Fabriano's paper mills were produci

and when i show up is: id is missing

modifier 2015-09-10 10:51:13
id 
firstlevel2
second level1
latitude 39.679869,20.872725
latitude 39.679869
longtitude 20.872725

The json sample from json viewer is the below format

root        {1}

    array       {4}

respond :   1

    paging      {7}

stillmore   :   0

perpage :   150

callpage    :   1

next    :   2

previous    :   0

pages   :   1

result  :   103

message :   

    result      [103]

    0       {26}

ID  :   5712

post_title  :   Fabriano

guid    :   http:/bla bla bla  pe=listing&p=5712

post_content    :

So, why the ID is missing? Is the ID expression of c#?


Solution

  • The problem is that ID is a private field of your class, and doesn't have a public property. You'll need to do:

    public string ID { get; set; }
    

    As a side note, you can save yourself all the private field declarations (and the verbosiness that comes with it) by using Auto-Implemented Properties, where the compiler generates a backing field for you:

    public string Status { get; set; }
    public string Count { get; set; }
    public string Pages { get; set; }
    public List<PointOfInterest> Posts { get; set; }