如何打印出json对象中的选定字段而不必为其创建类,此时它打印出整个json对象,但我只想打印出选定的字段,例如Console.WriteLine( response.venues.name);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace FourSquareTest
{
class Program
{
static void Main(string[] args)
{
using (var webClient = new System.Net.WebClient())
{
var json = webClient.DownloadString("https://api.foursquare.com/v2/venues/search?ll=40.7,-74&query=mcdonalds&client_id=XXXXXXX&client_secret=XXXXXXXX&v=20120101");
// Now parse with JSON.Net
JObject parsed = JObject.Parse(json);
foreach (var pair in parsed)
{
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
}
}
}
}
}
答案 0 :(得分:1)
尝试
JObject parsed = JObject.Parse(json);
JToken response = parsed["response"];
JArray venues = (JArray)response["venues"];
JValue names = (JValue)venues[1]["name"];
但是我没有要测试的库,所以这只是基于文档。