Reputation:
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"
},
Upvotes: 13
Views: 52779
Reputation: 31
public class DataObj
{
[JsonProperty("prop_1")]
public string Prop1{ get; set; }
}
//Select is from Linq
var data = e.details.Select(a => a.ToObject<DataObj>()); // e.details is a JArray
//json is like -----
{
"prop_1": "text"
}
Upvotes: 0
Reputation: 52366
Here is an example of how to get JToken and JArray as a string.
Use Formatting.None for simple formatting.
string json = "[\"a\", null, \"b\", null, null, null, 0,[],[[\"c\"], null,[0],[\"d\"]]]";
JArray array = JArray.Parse(json);
// array string
string arrayStr = array.ToString(Newtonsoft.Json.Formatting.None);
for (int i = 0; i < array.Count; i++)
{
JToken elem = array[i];
// token string
string jtokenStr = elem.ToString(Newtonsoft.Json.Formatting.None);
}
Upvotes: 5
Reputation: 129677
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);
Upvotes: 13
Reputation: 1254
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
Upvotes: 0