3

如果我在这里真的很愚蠢,我会提前道歉,但是我找不到正确的语法来从 JSON 返回中提取一些数据。以下是返回的 JSON 数据:

    {
"version":"1.0",
"encoding":"UTF-8",
"feed":{
"xmlns":"http://www.w3.org/2005/Atom",
"xmlns$openSearch":"http://a9.com/-/spec/opensearchrss/1.0/",
"xmlns$gsx":"http://schemas.google.com/spreadsheets/2006/extended",
"id":{
"$t":"https://spreadsheets.google.com/feeds/list/0AhySzEddwIC1dEN6bnNQYkRlVE50RlBRLUQ5YlZhNUE/1/public/basic"
},
"updated":{
"$t":"2012-12-03T10:33:13.778Z"
},
"category":[
{
"scheme":"http://schemas.google.com/spreadsheets/2006",
"term":"http://schemas.google.com/spreadsheets/2006#list"
}
],
"title":{
"type":"text",
"$t":"Sheet1"
},
"link":[
{
"rel":"alternate",
"type":"text/html",
"href":"https://spreadsheets.google.com/pub?key\u003d0AhySzEddwIC1dEN6bnNQYkRlVE50RlBRLUQ5YlZhNUE"
},
{
"rel":"http://schemas.google.com/g/2005#feed",
"type":"application/atom+xml",
"href":"https://spreadsheets.google.com/feeds/list/0AhySzEddwIC1dEN6bnNQYkRlVE50RlBRLUQ5YlZhNUE/1/public/basic"
},
{
"rel":"self",
"type":"application/atom+xml",
"href":"https://spreadsheets.google.com/feeds/list/0AhySzEddwIC1dEN6bnNQYkRlVE50RlBRLUQ5YlZhNUE/1/public/basic?alt\u003djson"
}
],
"author":[
{
"name":{
"$t":"rourkie"
},
"email":{
"$t":"rourkie@gmail.com"
}
}
],
"openSearch$totalResults":{
"$t":"1"
},
"openSearch$startIndex":{
"$t":"1"
},
"entry":[
{
"id":{
"$t":"https://spreadsheets.google.com/feeds/list/0AhySzEddwIC1dEN6bnNQYkRlVE50RlBRLUQ5YlZhNUE/1/public/basic/cn6ca"
},
"updated":{
"$t":"2012-12-03T10:33:13.778Z"
},
"category":[
{
"scheme":"http://schemas.google.com/spreadsheets/2006",
"term":"http://schemas.google.com/spreadsheets/2006#list"
}
],
"title":{
"type":"text",
"$t":"5872.64"
},
"content":{
"type":"text",
"$t":"change: 3.6"
},
"link":[
{
"rel":"self",
"type":"application/atom+xml",
"href":"https://spreadsheets.google.com/feeds/list/0AhySzEddwIC1dEN6bnNQYkRlVE50RlBRLUQ5YlZhNUE/1/public/basic/cn6ca"
}
]
}
]
}
}

我正在尝试使用以下内容提取“更改”数字:

feed.entry[4].content.$t

但它只是不断返回一个错误。

谁能阐明我做错了什么?

谢谢

4

2 回答 2

2

JSONLint - http://jsonlint.com/ - 非常方便。

您发布的 JSON 在 entry 数组中只有一个对象(除非您只是将其发布为示例)......所以它将是:

feed.entry[0].content.$t
于 2012-12-05T16:17:36.920 回答
0

使用 json2csharp.com ( http://json2csharp.com/ ) 您可以粘贴 json,它会为您提供与 json 匹配的类,让您轻松解析它。检查如何使用 C# 解析 JSON?关于如何使用 JsonConvert。我相信你可以在 nuget 包中找到它。请记住,由于 json 中某些字段的命名,我在下面粘贴的类不能直接工作。您可能必须手动重命名它们并映射它们。

public class Id
{
    public string __invalid_name__$t { get; set; }
}

public class Updated
{
    public string __invalid_name__$t { get; set; }
}

public class Category
{
    public string scheme { get; set; }
    public string term { get; set; }
}

public class Title
{
    public string type { get; set; }
    public string __invalid_name__$t { get; set; }
}

public class Link
{
    public string rel { get; set; }
    public string type { get; set; }
    public string href { get; set; }
}

public class Name
{
    public string __invalid_name__$t { get; set; }
}

public class Email
{
    public string __invalid_name__$t { get; set; }
}

public class Author
{
    public Name name { get; set; }
    public Email email { get; set; }
}

public class OpenSearchTotalResults
{
    public string __invalid_name__$t { get; set; }
}

public class OpenSearchStartIndex
{
    public string __invalid_name__$t { get; set; }
}

public class Id2
{
    public string __invalid_name__$t { get; set; }
}

public class Updated2
{
    public string __invalid_name__$t { get; set; }
}

public class Category2
{
    public string scheme { get; set; }
    public string term { get; set; }
}

public class Title2
{
    public string type { get; set; }
    public string __invalid_name__$t { get; set; }
}

public class Content
{
    public string type { get; set; }
    public string __invalid_name__$t { get; set; }
}

public class Link2
{
    public string rel { get; set; }
    public string type { get; set; }
    public string href { get; set; }
}

public class Entry
{
    public Id2 id { get; set; }
    public Updated2 updated { get; set; }
    public List<Category2> category { get; set; }
    public Title2 title { get; set; }
    public Content content { get; set; }
    public List<Link2> link { get; set; }
}

public class Feed
{
    public string xmlns { get; set; }
    public string __invalid_name__xmlns$openSearch { get; set; }
    public string __invalid_name__xmlns$gsx { get; set; }
    public Id id { get; set; }
    public Updated updated { get; set; }
    public List<Category> category { get; set; }
    public Title title { get; set; }
    public List<Link> link { get; set; }
    public List<Author> author { get; set; }
    public OpenSearchTotalResults __invalid_name__openSearch$totalResults { get; set; }
    public OpenSearchStartIndex __invalid_name__openSearch$startIndex { get; set; }
    public List<Entry> entry { get; set; }
}

public class RootObject
{
    public string version { get; set; }
    public string encoding { get; set; }
    public Feed feed { get; set; }
}
于 2012-12-05T16:13:51.517 回答