Reputation: 12014
The json string in the variable content
I received is this :
"{\n \"predictions\" : [\n {\n \"description\" : \"9130 Beveren, Belgium\",\n \"id\" : \"96df4cd9b49e7ba4172485e91a4d268223886695\",\n \"matched_substrings\" : [\n {\n \"length\" : 4,\n \"offset\" : 0\n },\n
{\n \"length\" : 7,\n \"offset\" : 14\n }\n ],\n \"place_id\" : \"ChIJk9NyZm8IxEcRDuOa4IbwK9Q\",\n \"reference\" : \"ChIJk9NyZm8IxEcRDuOa4IbwK9Q\",\n \"structured_formatting\" : {\n \"main_text\" : \"9130\",
\n \"main_text_matched_substrings\" : [\n {\n \"length\" : 4,\n \"offset\" : 0\n }\n ],\n \"secondary_text\" : \"Beveren, Belgium\",\n \"secondary_text_matched_substrings\" : [\n {\n
\"length\" : 7,\n \"offset\" : 9\n }\n ]\n },\n \"terms\" : [\n {\n \"offset\" : 0,\n \"value\" : \"9130\"\n },\n {\n \"offset\" : 5,\n \"value\" : \"Beveren\"\n },\n {\n \"offset\" : 14,\n \"value\" : \"Belgium\"\n }\n ],\n \"types\" : [ \"postal_code\", \"geocode\" ]\n }\n ],\n \"status\" : \"OK\"\n}\n"
Same JSON with formatting for better readability:
{
predictions : [
{
"description" : "9130 Beveren, Belgium",
"id" : "96df4cd9b49e7ba4172485e91a4d268223886695",
"matched_substrings" : [
{
"length" : 4,
"offset" : 0
},
{
"length" : 7,
"offset" : 14
}
],
"place_id" : "ChIJk9NyZm8IxEcRDuOa4IbwK9Q",
"reference" : "ChIJk9NyZm8IxEcRDuOa4IbwK9Q",
"structured_formatting" : {
"main_text" : "9130",
"main_text_matched_substrings" : [
{
"length" : 4,
"offset" : 0
}
],
"secondary_text" : "Beveren, Belgium",
"secondary_text_matched_substrings" : [
{
"length" : 7,
"offset" : 9
}
]
},
"terms" : [
{
"offset" : 0,
"value" : "9130"
},
{
"offset" : 5,
"value" : "Beveren"
},
{
"offset" : 14,
"value" : "Belgium"
}
],
"types" : [ "postal_code", "geocode" ]
}
],
"status" : "OK"
}
I need to retrieve the value for the place_id
but I cant figure out how to do it.
What I expect as result is ChIJk9NyZm8IxEcRDuOa4IbwK9Q
I tried this
dynamic data = JObject.Parse(content);
string result = data["place_id"].ToString();
and this
string result = data["predictions.place_id"].ToString();
but they both return nothing.
If I do data ["predictions"] it returns something that has 8 rows, and I can see the variable ´place_id` with its value, but I dont known how to extract it.
What is the proper and fastest way of retrieving this value ?
Upvotes: 1
Views: 111
Reputation: 587
Using your json
{
predictions : [
{
"description" : "9130 Beveren, Belgium",
"id" : "96df4cd9b49e7ba4172485e91a4d268223886695",
"matched_substrings" : [
{
"length" : 4,
"offset" : 0
},
{
"length" : 7,
"offset" : 14
}
],
"place_id" : "ChIJk9NyZm8IxEcRDuOa4IbwK9Q",
"reference" : "ChIJk9NyZm8IxEcRDuOa4IbwK9Q",
"structured_formatting" : {
"main_text" : "9130",
"main_text_matched_substrings" : [
{
"length" : 4,
"offset" : 0
}
],
"secondary_text" : "Beveren, Belgium",
"secondary_text_matched_substrings" : [
{
"length" : 7,
"offset" : 9
}
]
},
"terms" : [
{
"offset" : 0,
"value" : "9130"
},
{
"offset" : 5,
"value" : "Beveren"
},
{
"offset" : 14,
"value" : "Belgium"
}
],
"types" : [ "postal_code", "geocode" ]
}
],
"status" : "OK"
}
You can use JsonConvert.DeserializeObject static method to deserliaze your json to a dynamic object then access its properties members, however using this method is risky because if the json gets changed the code will break.
dynamic obj = JsonConvert.DeserializeObject(json);
var palceId = obj.predictions[0].place_id; //ChIJk9NyZm8IxEcRDuOa4IbwK9Q
Upvotes: 1
Reputation: 3912
I suggest you to convert it to class format for better approach and more readability and maintainability. below is a sample class of your JSON data.
public class MatchedSubstring
{
public int length { get; set; }
public int offset { get; set; }
}
public class MainTextMatchedSubstring
{
public int length { get; set; }
public int offset { get; set; }
}
public class SecondaryTextMatchedSubstring
{
public int length { get; set; }
public int offset { get; set; }
}
public class StructuredFormatting
{
public string main_text { get; set; }
public IList<MainTextMatchedSubstring> main_text_matched_substrings { get; set; }
public string secondary_text { get; set; }
public IList<SecondaryTextMatchedSubstring> secondary_text_matched_substrings { get; set; }
}
public class Term
{
public int offset { get; set; }
public string value { get; set; }
}
public class Prediction
{
public string description { get; set; }
public string id { get; set; }
public IList<MatchedSubstring> matched_substrings { get; set; }
public string place_id { get; set; }
public string reference { get; set; }
public StructuredFormatting structured_formatting { get; set; }
public IList<Term> terms { get; set; }
public IList<string> types { get; set; }
}
public class Prediction
{
public IList<Prediction> predictions { get; set; }
public string status { get; set; }
}
Prediction predictionJSON = JsonConvert.DeserializeObject<Prediction>(jsonString);
string PlaceID=predictionJSON.predictions[0].place_id;
Upvotes: 0
Reputation: 270780
I used a json formatter to format your json:
{
"predictions":[
{
"description":"9130 Beveren, Belgium",
"id":"96df4cd9b49e7ba4172485e91a4d268223886695",
"matched_substrings":[
{
"length":4,
"offset":0
},
{
"length":7,
"offset":14
}
],
"place_id":"ChIJk9NyZm8IxEcRDuOa4IbwK9Q",
"reference":"ChIJk9NyZm8IxEcRDuOa4IbwK9Q",
"structured_formatting":{
"main_text":"9130",
"main_text_matched_substrings":[
{
"length":4,
"offset":0
}
],
"secondary_text":"Beveren, Belgium",
"secondary_text_matched_substrings":[
{
"length":7,
"offset":9
}
]
},
"terms":[
{
"offset":0,
"value":"9130"
},
{
"offset":5,
"value":"Beveren"
},
{
"offset":14,
"value":"Belgium"
}
],
"types":[
"postal_code",
"geocode"
]
}
],
"status":"OK"
}
predictions
is actually an array. You need to specify which element of prediction
before saying that you want the place_id
. In this case there is only one element, so you can do [0]
:
var result = data["predictions"][0]["place_id"];
But you might get multiple predictions in other cases. You need to decide which prediction you want the place id of.
Upvotes: 2