Newtonsoft Json Parser

时间:2013-03-12 05:06:10

标签: c# json

我如何解析来自https://graph.facebook.com/google/

的以下结果
{
   "about": "Organizing the world's information and making it universally accessible and useful.",
   "checkins": 22645,
   "company_overview": "Google is a public and profitable company focused on search services. Named for the mathematical term \"googol,\" Google operates web sites at many international domains, with the most trafficked being www.google.com. Google is widely recognized as the \"world's best search engine\" because it is fast, accurate and easy to use. The company also serves corporate clients, including advertisers, content publishers and site managers with cost-effective advertising and a wide range of revenue generating search services. Google's breakthrough technology and continued innovation serve the company's mission of \"organizing the world's information and making it universally accessible and useful.\"",
   "founded": "1998",
   "is_published": true,
   "location": {
      "street": "1600 Amphitheatre Parkway",
      "city": "Mountain View",
      "state": "CA",
      "country": "United States",
      "zip": "94043",
      "latitude": 37.421956357856,
      "longitude": -122.08422985089
   },
   "mission": "Google's mission is to organize the world's information and make it universally accessible and useful.",
   "products": "See a full list:\nhttp://www.google.com/options/index.html",
   "talking_about_count": 60684,
   "username": "Google",
   "website": "www.google.com",
   "were_here_count": 0,
   "category": "Website",
   "id": "104958162837",
   "name": "Google",
   "link": "http://www.facebook.com/Google",
   "likes": 12341682,
   "cover": {
      "cover_id": "10151163547067838",
      "source": "http://sphotos-d.ak.fbcdn.net/hphotos-ak-ash3/s720x720/546101_10151163547067838_18950259_n.jpg",
      "offset_y": 0,
      "offset_x": 0
   }
}

2 个答案:

答案 0 :(得分:4)

http://json2csharp.com中粘贴JSON,它将为您提供映射的类:

public class Location
{
    public string street { get; set; }
    public string city { get; set; }
    public string state { get; set; }
    public string country { get; set; }
    public string zip { get; set; }
    public double latitude { get; set; }
    public double longitude { get; set; }
}

public class Cover
{
    public string cover_id { get; set; }
    public string source { get; set; }
    public int offset_y { get; set; }
    public int offset_x { get; set; }
}

public class RootObject
{
    public string about { get; set; }
    public int checkins { get; set; }
    public string company_overview { get; set; }
    public string founded { get; set; }
    public bool is_published { get; set; }
    public Location location { get; set; }
    public string mission { get; set; }
    public string products { get; set; }
    public int talking_about_count { get; set; }
    public string username { get; set; }
    public string website { get; set; }
    public int were_here_count { get; set; }
    public string category { get; set; }
    public string id { get; set; }
    public string name { get; set; }
    public string link { get; set; }
    public int likes { get; set; }
    public Cover cover { get; set; }
}

稍后您可以将Newtonsoft JSON解析器用作:

RootObject myObject =  JsonConvert.DeserializeObject<RootObject>(jsonString);

您应该看到documentation for Json.Net

答案 1 :(得分:1)

我更喜欢创建和支持多个表示json结构的类,而不是将json反序列化为动态对象:

dynamic d = JObject.Parse(json);
//d.founded == "1998"