如何从JToken获取字符串值

时间:2021-12-28 00:41:38

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

我从一个返回JSON响应的Web服务获取数据。这是我的代码:

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:

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

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

无法将类型'Newtonsoft.Json.Linq.JToken'隐式转换为'string'

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

我想帮助将返回的数据导入模型中的变量。

This is my JSON:

这是我的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"
},

2 个解决方案

#1


4  

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.

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

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

你可以像这样得到这个城市,其中i是你想要的位置的索引:

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

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:

但是,如果你所做的只是填充模型对象,那么直接反序列化到使用JsonConvert.DeserializeObject 的那些对象可能更简单,而不是使用JTokens手动填充它们。例如,如果您的类定义如下:

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);

#2


0  

You can add those class models to your app, and deserialize the json to it, you can use various types of deseriallizers, personally I like newtonsoft's json.net, here are the classes:

你可以将这些类模型添加到你的应用程序,并将json反序列化为它,你可以使用各种类型的deseriallizers,我个人喜欢newtonsoft的json.net,这里是类:

public class AddressObj
{
    public string street1 { get; set; }
    public string street2 { get; set; }
    public string city { get; set; }
    public string state { get; set; }
    public string country { get; set; }
    public string postalcode { get; set; }
    public string address_string { get; set; }
}

public class Datum
{
    public AddressObj address_obj { get; set; }
}

public class RootObject
{
    public List<Datum> data { get; set; }
}

After that deserialize the response to AddressObj and access the city property

之后反序列化对AddressObj的响应并访问city属性

#1


4  

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.

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

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

你可以像这样得到这个城市,其中i是你想要的位置的索引:

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

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:

但是,如果你所做的只是填充模型对象,那么直接反序列化到使用JsonConvert.DeserializeObject 的那些对象可能更简单,而不是使用JTokens手动填充它们。例如,如果您的类定义如下:

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);

#2


0  

You can add those class models to your app, and deserialize the json to it, you can use various types of deseriallizers, personally I like newtonsoft's json.net, here are the classes:

你可以将这些类模型添加到你的应用程序,并将json反序列化为它,你可以使用各种类型的deseriallizers,我个人喜欢newtonsoft的json.net,这里是类:

public class AddressObj
{
    public string street1 { get; set; }
    public string street2 { get; set; }
    public string city { get; set; }
    public string state { get; set; }
    public string country { get; set; }
    public string postalcode { get; set; }
    public string address_string { get; set; }
}

public class Datum
{
    public AddressObj address_obj { get; set; }
}

public class RootObject
{
    public List<Datum> data { get; set; }
}

After that deserialize the response to AddressObj and access the city property

之后反序列化对AddressObj的响应并访问city属性