[英]Get a value from JSON from c#
i am trying to get a value from a json file using c#.我正在尝试使用 c# 从 json 文件中获取值。 the json file looks more like the below string. 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"
]
}
]
}
I am looking for the formatted_address element from the json from C#.我正在从 C# 的 json 中寻找formatted_address元素。 I am getting lost in JObject, JArray, JToken.我在 JObject、JArray、JToken 中迷失了方向。 I am trying to use NewtonSoft JSON.我正在尝试使用 NewtonSoft JSON。 Thank you.谢谢你。
Using the LINQ-to-JSON API (JObjects) you can get the formatted address easily using the SelectToken
method:使用LINQ-to-JSON API (JObjects),您可以使用SelectToken
方法轻松获取格式化地址:
JObject obj = JObject.Parse(json);
string address = (string)obj.SelectToken("results[0].formatted_address");
Console.WriteLine(address);
Fiddle: https://dotnetfiddle.net/Fdvqkl小提琴: https : //dotnetfiddle.net/Fdvqkl
The easiest way would be to have model objects and then call最简单的方法是拥有模型对象,然后调用
var rootObject = JsonConvert.DeserializeObject(jsonString);
With jsonString being your input.使用 jsonString 作为您的输入。 Then you can retrieve the formatted_address from the first array element like this like this:然后你可以像这样从第一个数组元素中检索 formatted_address :
var formattedAddress = rootObject.results[0].formatted_address;
Hope it helps.希望能帮助到你。
using Newtonsoft.Json
nuget .使用Newtonsoft.Json
nuget 。 and do something like that并做类似的事情
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;
}
}
}
change JTokenType.Array
to whatever the type u wish , also change the "results" to the property name you wish to extract将JTokenType.Array
更改为您希望的任何类型,还将“结果”更改为您希望提取的属性名称
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.