3

I will appologise in advance if I am being really stupid here, but I cant find the right syntax to extract some data from a JSON return. The following is the returned JSON data:

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

I am trying to extract the "change" figure, with the following:

feed.entry[4].content.$t

but it just keeps returning an error.

Can anyone shed some light on what I am doing wrong??

Thanks

2 Answers2

2

JSONLint - http://jsonlint.com/ - is pretty handy for this.

The JSON you posted only has one object in the entry array (unless you just posted it as an example)...so it would be:

feed.entry[0].content.$t
Adam Jenkins
  • 51,445
  • 11
  • 72
  • 100
  • In addition to JSONLint, may I recommend [http://jsonviewer.stack.hu](http://jsonviewer.stack.hu) as a great way to view the tree of a JSON object. – brichins May 30 '13 at 16:06
0

Using json2csharp.com (http://json2csharp.com/) You can paste in json and it will give you a class matching the json allowing you to easily parse it. Check How can I parse JSON with C#? on how to use JsonConvert. I believe you can find it in nuget packages. Keep in mind the class I have pasted below will not work directly, because of the naming of some of the fields in the json. You may have to rename them manually and map them.

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; }
}
Community
  • 1
  • 1
Pete Garafano
  • 4,863
  • 1
  • 23
  • 40