为什么不从json c#中获取ID字段?

时间:2015-09-24 08:08:19

标签: c# json windows-phone json.net

我正在尝试使用库newtonsoft从json获取ID,但ID为null,而其他字段是正确的(带内容)。 课程是:

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;
}

我得到的json是

{"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

当我出现时:id缺失

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

来自json viewer的json示例是以下格式

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    :

那么,ID为何缺失?是c#?

的ID表达式

1 个答案:

答案 0 :(得分:1)

问题是,ID是您班级的private字段,并且没有public属性。你需要这样做:

public string ID { get; set; }

作为旁注,您可以使用Auto-Implemented Properties保存自己的所有私有字段声明(及其附带的冗长声明),编译器为您生成支持字段:

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