从c#中获取JSON的值

时间:2016-09-25 08:51:07

标签: c# json json.net

我正在尝试使用c#从json文件中获取值。 json文件看起来更像下面的字符串。

{
    "results": [
        {
            "address_components": [
                {
                    "long_name": "3",
                    "short_name": "3",
                    "types": [
                        "street_number"
                    ]
                }
            ],
            "formatted_address": "3, Puppalaguda - Manikonda Main Rd, Sri Laxmi Nagar Colony, Manikonda, Hyderabad, Telangana 500089, India",
            "geometry": {
                "bounds": {
                    "northeast": {
                        "lat": 17.4025788,
                        "lng": 78.3748307
                    },
                    "southwest": {
                        "lat": 17.4019665,
                        "lng": 78.3733937
                    }
                },
                "location": {
                    "lat": 17.4023166,
                    "lng": 78.37417409999999
                },
                "location_type": "RANGE_INTERPOLATED",
                "viewport": {
                    "northeast": {
                        "lat": 17.4036216302915,
                        "lng": 78.37546118029151
                    },
                    "southwest": {
                        "lat": 17.4009236697085,
                        "lng": 78.3727632197085
                    }
                }
            },
            "place_id": "EmkzLCBQdXBwYWxhZ3VkYSAtIE1hbmlrb25kYSBNYWluIFJkLCBTcmkgTGF4bWkgTmFnYXIgQ29sb255LCBNYW5pa29uZGEsIEh5ZGVyYWJhZCwgVGVsYW5nYW5hIDUwMDA4OSwgSW5kaWE",
            "types": [
                "street_address"
            ]
        }
    ]
}

我正在寻找C#中json的 formatted_address 元素。我迷失在JObject,JArray,JToken中。我正在尝试使用NewtonSoft JSON。谢谢。

3 个答案:

答案 0 :(得分:0)

最简单的方法是拥有模型对象,然后调用

var rootObject = JsonConvert.DeserializeObject(jsonString); 

以jsonString为输入。然后你可以像这样从第一个数组元素中检索formatted_address:

var formattedAddress = rootObject.results[0].formatted_address;

希望它有所帮助。

答案 1 :(得分:0)

使用LINQ-to-JSON API(JObjects),您可以使用SelectToken方法轻松获取格式化地址:

JObject obj = JObject.Parse(json);
string address = (string)obj.SelectToken("results[0].formatted_address");
Console.WriteLine(address);

小提琴:https://dotnetfiddle.net/Fdvqkl

答案 2 :(得分:-1)

使用Newtonsoft.Json nuget。并做那样的事情

 static void Main(string[] args)
    {
        string json = "{'results':[{'SwiftCode':'','City':'','BankName':'Deutsche    Bank','Bankkey':'10020030','Bankcountry':'DE'},{'SwiftCode':'','City':'10891    Berlin','BankName':'Commerzbank Berlin (West)','Bankkey':'10040000','Bankcountry':'DE'}]}";

        var resultObjects = AllChildren(JObject.Parse(json))
            .First(c => c.Type == JTokenType.Array && c.Path.Contains("results"))
            .Children<JObject>();

        foreach (JObject result in resultObjects)
        {
            foreach (JProperty property in result.Properties())
            {
                // do something with the property belonging to result
            }
        }
    }

    // recursively yield all children of json
    private static IEnumerable<JToken> AllChildren(JToken json)
    {
        foreach (var c in json.Children())
        {
            yield return c;
            foreach (var cc in AllChildren(c))
            {
                yield return cc;
            }
        }
    }

JTokenType.Array更改为您希望的类型,也可以更改&#34;结果&#34;到您要提取的属性名称