如何提取json数据

时间:2015-04-24 08:18:47

标签: c# asp.net json

我有一些想要检索的流数据 来自以下数据的if($_SERVER['HTTP_X_REQUESTED_WITH'] == "com.company.app") echo 'var isAndroidApp=true;'; else echo 'var isAndroidApp=false;';

如何从这种方法中获得399的价值   解析JSON - > responseData - >光标 - > estimatedResultCount将为您提供支持计数。

"estimatedResultCount":"399"

代码

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

1 个答案:

答案 0 :(得分: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; }
    }