Newtonsoft.Json解析,JObject、JArray的联系

  • 构建有json字符串,字符串如下格式:{
    “images”:[{},{},{}],
    “tooltips”:{}
    }
    其中 images为json数组
{"images":[{"startdate":"20171130","fullstartdate":"201711301600","enddate":"20171201","url":"/az/hprichbg/rb/SpiralSpain_ZH-CN12059815472_1920x1080.jpg","urlbase":"/az/hprichbg/rb/SpiralSpain_ZH-CN12059815472","copyright":"索菲亚王后艺术歌剧院的内部,西班牙巴伦西亚 (? AWL Images/Masterfile)","copyrightlink":"http://www.bing.com/search?q=%E7%B4%A2%E8%8F%B2%E4%BA%9A%E7%8E%8B%E5%90%8E%E8%89%BA%E6%9C%AF%E6%AD%8C%E5%89%A7%E9%99%A2&form=hpcapt&mkt=zh-cn","quiz":"/search?q=Bing+homepage+quiz&filters=WQOskey:%22HPQuiz_20171130_SpiralSpain%22&FORM=HPQUIZ","wp":false,"hsh":"ac6fd4985a28ad68a9d6efbd4720d7e1","drk":1,"top":1,"bot":1,"hs":[]},{"startdate":"20171129","fullstartdate":"201711291600","enddate":"20171130","url":"/az/hprichbg/rb/Piraputanga_ZH-CN13303102627_1920x1080.jpg","urlbase":"/az/hprichbg/rb/Piraputanga_ZH-CN13303102627","copyright":"塞拉多生态区内的希氏石脂鲤,巴西 (? Luciano Candisani/Minden Pictures)","copyrightlink":"http://www.bing.com/search?q=%E5%B8%8C%E6%B0%8F%E7%9F%B3%E8%84%82%E9%B2%A4&form=hpcapt&mkt=zh-cn","quiz":"/search?q=Bing+homepage+quiz&filters=WQOskey:%22HPQuiz_20171129_Piraputanga%22&FORM=HPQUIZ","wp":false,"hsh":"72a276ee530bcc590a067c96a0829d46","drk":1,"top":1,"bot":1,"hs":[]},{"startdate":"20171128","fullstartdate":"201711281600","enddate":"20171129","url":"/az/hprichbg/rb/LAUnionStation_ZH-CN8034711319_1920x1080.jpg","urlbase":"/az/hprichbg/rb/LAUnionStation_ZH-CN8034711319","copyright":"洛杉矶的联合车站,美国加利福尼亚州 (? Ted Vancleave/Gallery Stock)","copyrightlink":"http://www.bing.com/search?q=%E8%81%94%E5%90%88%E8%BD%A6%E7%AB%99&form=hpcapt&mkt=zh-cn","quiz":"/search?q=Bing+homepage+quiz&filters=WQOskey:%22HPQuiz_20171128_LAUnionStation%22&FORM=HPQUIZ","wp":false,"hsh":"1c4b531fdc9108bcc2c3639dd8257c29","drk":1,"top":1,"bot":1,"hs":[]},{"startdate":"20171127","fullstartdate":"201711271600","enddate":"20171128","url":"/az/hprichbg/rb/RiceLights_ZH-CN7549259687_1920x1080.jpg","urlbase":"/az/hprichbg/rb/RiceLights_ZH-CN7549259687","copyright":"灯光装点的水稻梯田,日本轮岛 (? Sean Pavone/Alamy)","copyrightlink":"http://www.bing.com/search?q=%E6%B0%B4%E7%A8%BB%E6%A2%AF%E7%94%B0&form=hpcapt&mkt=zh-cn","quiz":"/search?q=Bing+homepage+quiz&filters=WQOskey:%22HPQuiz_20171127_RiceLights%22&FORM=HPQUIZ","wp":false,"hsh":"3928c714b480ec1f3df0ffaa5d25e306","drk":1,"top":1,"bot":1,"hs":[]},{"startdate":"20171126","fullstartdate":"201711261600","enddate":"20171127","url":"/az/hprichbg/rb/BFBadger_ZH-CN8490916760_1920x1080.jpg","urlbase":"/az/hprichbg/rb/BFBadger_ZH-CN8490916760","copyright":"狗獾在黑森林中觅食,德国 (? Klaus Echle/Minden Pictures)","copyrightlink":"http://www.bing.com/search?q=%E7%8B%97%E7%8D%BE&form=hpcapt&mkt=zh-cn","quiz":"/search?q=Bing+homepage+quiz&filters=WQOskey:%22HPQuiz_20171126_BFBadger%22&FORM=HPQUIZ","wp":false,"hsh":"b053ed696c1ea4fa27620dac8e690e4b","drk":1,"top":1,"bot":1,"hs":[]},{"startdate":"20171125","fullstartdate":"201711251600","enddate":"20171126","url":"/az/hprichbg/rb/RRCNCA_ZH-CN8115353106_1920x1080.jpg","urlbase":"/az/hprichbg/rb/RRCNCA_ZH-CN8115353106","copyright":"红岩峡谷国家野生生物保护区,美国内华达州 (? plainpicture/Westend61/Cameron Davidson)","copyrightlink":"http://www.bing.com/search?q=%E7%BA%A2%E5%B2%A9%E5%B3%A1%E8%B0%B7%E5%9B%BD%E5%AE%B6%E9%87%8E%E7%94%9F%E7%94%9F%E7%89%A9%E4%BF%9D%E6%8A%A4%E5%8C%BA&form=hpcapt&mkt=zh-cn","quiz":"/search?q=Bing+homepage+quiz&filters=WQOskey:%22HPQuiz_20171125_RRCNCA%22&FORM=HPQUIZ","wp":false,"hsh":"454f09afecef25db6413ad95a664358c","drk":1,"top":1,"bot":1,"hs":[]},{"startdate":"20171124","fullstartdate":"201711241600","enddate":"20171125","url":"/az/hprichbg/rb/RhoneRiverDusk_ZH-CN7956980058_1920x1080.jpg","urlbase":"/az/hprichbg/rb/RhoneRiverDusk_ZH-CN7956980058","copyright":"圣贝内泽桥和罗讷河黄昏时刻的美景,法国阿维尼翁 (? David Noton/Minden Pictures)","copyrightlink":"http://www.bing.com/search?q=%E5%9C%A3%E8%B4%9D%E5%86%85%E6%B3%BD%E6%A1%A5&form=hpcapt&mkt=zh-cn","quiz":"/search?q=Bing+homepage+quiz&filters=WQOskey:%22HPQuiz_20171124_RhoneRiverDusk%22&FORM=HPQUIZ","wp":false,"hsh":"752a7976fb7edb91858558e8194ac92d","drk":1,"top":1,"bot":1,"hs":[]},{"startdate":"20171123","fullstartdate":"201711231600","enddate":"20171124","url":"/az/hprichbg/rb/KatenaLuminarium_ZH-CN12074286571_1920x1080.jpg","urlbase":"/az/hprichbg/rb/KatenaLuminarium_ZH-CN12074286571","copyright":"由Architects of Air制造的充气雕塑“Katena Luminarium” (? Shirlaine Forrest/WireImage/Getty Images)","copyrightlink":"http://www.bing.com/search?q=Architects+of+Air&form=hpcapt&mkt=zh-cn","quiz":"/search?q=Bing+homepage+quiz&filters=WQOskey:%22HPQuiz_20171123_KatenaLuminarium%22&FORM=HPQUIZ","wp":false,"hsh":"b52c410e8206b56e94dc1b619f25075b","drk":1,"top":1,"bot":1,"hs":[]}],
"tooltips":{"loading":"正在加载...","previous":"上一个图像","next":"下一个图像","walle":"此图片不能下载用作壁纸。","walls":"下载今日美图。仅限用作桌面壁纸。"}}

处理这类json数据,首先获取到它,通过JsonConvert.DeserializeObject转化为JObject对象,然后通过JArray jArray = JArray.Parse(jo[“images”].ToString());获取第一个元素也就是images数组,然后处理json数组:

       List list = new List { };
        string postContent = GetPostInfo(url);
            JObject jo = (JObject)JsonConvert.DeserializeObject(postContent);

            //获取第一个json数组
            JArray jArray = JArray.Parse(jo["images"].ToString());
            string[] urls = new string[jArray.Count];
            int count = 0;
            foreach (var item in jArray)
            {
                ImageInfo imageInfo = new ImageInfo();
                imageInfo.Url= url0 + item["url"].ToString(); 
                imageInfo.Startdate= item["startdate"].ToString();
                imageInfo.Copyright= item["copyright"].ToString();
                list.Add(imageInfo);
                //urls[count++] = url0 + item["url"].ToString();
            }
            return list;
        }

这里自己做了一个类ImageInfo,用于存储所需要的信息,将json中所需的提取出来。

你可能感兴趣的:(c#)