2015-04-24 6 views
0

У меня есть поток данных от я хочу, чтобы получить "estimatedResultCount":"399" от этого ниже данныхкак извлекать данные JSon

как получить 399 значение из этого подхода Разбор JSON -> responseData -> курсор -> estimatedResultCount вы получите счет поддержки.

{ 
     "responseData": { 
      "results": [ 
       { 
        "GsearchResultClass": "GwebSearch", 
        "unescapedUrl": "http://www.homeocare.in/", 
        "url": "http://www.homeocare.in/", 
        "visibleUrl": "www.homeocare.in", 
        "cacheUrl": "http://www.google.com/search?q=cache:E7xF9dtFWgIJ:www.homeocare.in", 
        "title": "Homeopathy Clinics in Hyderabad - Homeopathy Treatment", 
        "titleNoFormatting": "Homeopathy Clinics in Hyderabad - Homeopathy Treatment", 
        "content": "Homeocare International - World Class Homeopathy Clinic in India provides \ninformation on causes, symptoms, and Homeopathy treatment of various \ndiseases." 
       }, 
       { 
        "GsearchResultClass": "GwebSearch", 
        "unescapedUrl": "http://www.homeocare.in/contactus.html", 
        "url": "http://www.homeocare.in/contactus.html", 
        "visibleUrl": "www.homeocare.in", 
        "cacheUrl": "http://www.google.com/search?q=cache:zrjNDKonZY0J:www.homeocare.in", 
        "title": "Homeocare International | Contact Us", 
        "titleNoFormatting": "Homeocare International | Contact Us", 
        "content": "To get best Homeopathy treatment Contact Homeocare International through \nphone or fill contact form. We have branches all over South India." 
       }, 
       { 
        "GsearchResultClass": "GwebSearch", 
        "unescapedUrl": "http://www.homeocare.in/testimonials.html", 
        "url": "http://www.homeocare.in/testimonials.html", 
        "visibleUrl": "www.homeocare.in", 
        "cacheUrl": "http://www.google.com/search?q=cache:oPJKzW6sHOkJ:www.homeocare.in", 
        "title": "Homeocare International Reviews/Testimonials", 
        "titleNoFormatting": "Homeocare International Reviews/Testimonials", 
        "content": "Homeocare International Reviews/Testimonials for Happy Patient. We pride \nourselves on excellent service, and our reviews show it!" 
       }, 
       { 
        "GsearchResultClass": "GwebSearch", 
        "unescapedUrl": "http://www.homeocare.in/arthritis.html", 
        "url": "http://www.homeocare.in/arthritis.html", 
        "visibleUrl": "www.homeocare.in", 
        "cacheUrl": "http://www.google.com/search?q=cache:gqHnM_Tg0hcJ:www.homeocare.in", 
        "title": "Homeopathy Treatment for Arthritis | Arthritis Treatment", 
        "titleNoFormatting": "Homeopathy Treatment for Arthritis | Arthritis Treatment", 
        "content": "Get Homeopathy Treatment for arthritis and rheumatoid arthritis at Homeocare \nInternational provides safe and effective remedies with no side effects." 
       } 
      ], 
      "cursor": { 
       "resultCount": "399", 
       "pages": [ 
        { 
         "start": "0", 
         "label": 1 
        }, 
        { 
         "start": "4", 
         "label": 2 
        }, 
        { 
         "start": "8", 
         "label": 3 
        }, 
        { 
         "start": "12", 
         "label": 4 
        }, 
        { 
         "start": "16", 
         "label": 5 
        }, 
        { 
         "start": "20", 
         "label": 6 
        }, 
        { 
         "start": "24", 
         "label": 7 
        }, 
        { 
         "start": "28", 
         "label": 8 
        } 
       ], 
       "estimatedResultCount": "399", 
       "currentPageIndex": 0, 
       "moreResultsUrl": "http://www.google.com/search?oe=utf8&ie=utf8&source=uds&start=0&hl=en&q=site:homeocare.in", 
       "searchResultTime": "0.09" 
      } 
     }, 
    "responseDetails": null, 
    "responseStatus": 200 
    } 

Код

string strurl = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=site:homeocare.in"; 
      StreamReader stream = objm.URLServerRequest(strurl); 
      string myResponse = stream.ReadToEnd(); 

    public StreamReader URLServerRequest(string url) 
     { 
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
      StreamReader stream = new StreamReader(request.GetResponse().GetResponseStream()); 
      return stream; 
     } 

ответ

3

Вы можете создать класс здесь http://json2csharp.com/, это очень легко.

Учитывая, что вы успешно загрузили строку json, вот код в качестве консольного приложения. Он использует json.net (https://www.nuget.org/packages/Newtonsoft.Json/7.0.1-beta2)

class Program 
    { 
     static void Main(string[] args) 
     { 
      var json = @".... json string...."; 
      var obj = JsonConvert.DeserializeObject<RootObject>(json); 
      Console.WriteLine(obj.responseData.cursor.estimatedResultCount) 
       ; 
      Console.ReadLine(); 
     } 
    } 
    public class Result 
    { 
     public string GsearchResultClass { get; set; } 
     public string unescapedUrl { get; set; } 
     public string url { get; set; } 
     public string visibleUrl { get; set; } 
     public string cacheUrl { get; set; } 
     public string title { get; set; } 
     public string titleNoFormatting { get; set; } 
     public string content { get; set; } 
    } 
    public class Page 
    { 
     public string start { get; set; } 
     public int label { get; set; } 
    } 
    public class Cursor 
    { 
     public string resultCount { get; set; } 
     public List<Page> pages { get; set; } 
     public string estimatedResultCount { get; set; } 
     public int currentPageIndex { get; set; } 
     public string moreResultsUrl { get; set; } 
     public string searchResultTime { get; set; } 
    } 
    public class ResponseData 
    { 
     public List<Result> results { get; set; } 
     public Cursor cursor { get; set; } 
    } 
    public class RootObject 
    { 
     public ResponseData responseData { get; set; } 
     public object responseDetails { get; set; } 
     public int responseStatus { get; set; } 
    } 
+0

может я применять его в веб-приложения, я просто нужна строка estimatedResultCount общественности {получить; задавать; } @Paolo Coasta –

+0

Несомненно, просто извлеките код в функции, и вы можете вызвать его из любого места, где бы вы ни хотели –

+0

@PaoloCosta, вы использовали http://json2csharp.com/ для создания структуры класса? Если да, включите его в свой ответ, пожалуйста, чтобы будущие читатели знали, как их легко сгенерировать :) –

Смежные вопросы