How to get a string value from a JToken

2019-04-29 07:37发布

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:

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.

This is my 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条回答
Animai°情兽
2楼-- · 2019-04-29 08:01

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:

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

查看更多
叛逆
3楼-- · 2019-04-29 08:12

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.

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"];

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);
查看更多
登录 后发表回答