如何从JToken获取字符串值 [英] How to get a string value from a JToken

查看:1428
本文介绍了如何从JToken获取字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Web服务获取数据,该服务返回JSON响应. 这是我的代码:

I'm getting data from a web service that returns a JSON response. This is my code:

WebClient client = new WebClient();
var result =  client.DownloadString("http://some url");

JObject obj = JObject.Parse(result);

// Location l = new Location();
//   l.city = obj["ad"][2]; error here

这时它返回一个结果,但是我得到一个错误:

At this point it returns a result, but I am getting an error:

不能将类型'Newtonsoft.Json.Linq.JToken'隐式转换为'string'

Cannot implicitly convert type 'Newtonsoft.Json.Linq.JToken' to 'string'

我希望获得一些帮助,以将返回的数据放入模型中的变量中.

I would like some assistance getting the returned data into my variable in the model.

这是我的JSON:

{
data: [
{
address_obj: {
street1: "9518 Front Beach Road",
street2: "",
city: "Panama City Beach",
state: "Florida",
country: "United States",
postalcode: "32407",
address_string: "9518 Front Beach Road, Panama City Beach, FL 32407"
},

推荐答案

JSON表示一个包含对象data数组的外部对象,每个项目包含一个address_obj对象,然后该对象具有字符串属性.因此,您使用的JToken索引器语法必须匹配该层次结构,包括使用正确的属性名称.另外,从JToken检索值时,需要将其转换为正确的类型.

The JSON represents an outer object containing a data array of objects, with each item containing an address_obj object which then has string properties. So the JToken indexer syntax you use has to match that hierarchy, including using the correct property names. Also, when retrieving the value from a JToken you need to cast it to the correct type.

您可以这样获得城市,其中i是您想要的位置的索引:

You can get the city like this, where i is the index of the location you want:

l.city = (string)obj["data"][i]["address_obj"]["city"];

但是,如果您要做的只是填充模型对象,则直接对使用JsonConvert.DeserializeObject<T>的对象进行反序列化可能比使用JTokens手动对其进行反序列化更为简单.例如,如果您的类是这样定义的:

However, if all you're doing is populating model objects, it is probably simpler to deserialize directly to those using JsonConvert.DeserializeObject<T> rather than manually populating them using JTokens. For example, if your classes are defined like this:

public class RootObject
{
    [JsonProperty("data")]
    public List<Item> Data { get; set; }
}

public class Item
{
    [JsonProperty("address_obj")]
    public Location Location { get; set; }
}

public class Location
{
    [JsonProperty("street1")]
    public string Street1 { get; set; }
    [JsonProperty("street2")]
    public string Street2 { get; set; }
    [JsonProperty("city")]
    public string City { get; set; }
    [JsonProperty("state")]
    public string State { get; set; }
    [JsonProperty("country")]
    public string Country { get; set; }
    [JsonProperty("postalcode")]
    public string PostalCode { get; set; }
    [JsonProperty("address_string")]
    public string FullAddress { get; set; }
}

然后您可以像这样直接对他们反序列化:

Then you can deserialize directly to them like this:

RootObject obj = JsonConvert.DeserializeObject<RootObject>(result);

这篇关于如何从JToken获取字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆