【问题标题】:why do not fetch the ID field from json c#?为什么不从 json c# 中获取 ID 字段?
【发布时间】:2015-09-24 08:08:18
【问题描述】:

我正在尝试使用库 newtonsoft 从 json 获取 ID,但 ID 为空,而其他字段正确(包含内容)。 类是:

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表达式吗?

【问题讨论】:

  • 只有当它是属性类型时才会分配它。

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


【解决方案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; }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-17
    • 1970-01-01
    • 1970-01-01
    • 2011-11-18
    • 2021-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多