cant get a value from a json string

时间:2018-12-03 13:15:16

标签: c# json

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 ?

3 个答案:

答案 0 :(得分:2)

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.

答案 1 :(得分:1)

使用您的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"
}

您可以使用JsonConvert.DeserializeObject静态方法将json解串为动态对象,然后访问其属性成员,但是使用此方法是有风险的,因为如果json更改,则代码将中断。

 dynamic obj = JsonConvert.DeserializeObject(json);
 var palceId = obj.predictions[0].place_id;  //ChIJk9NyZm8IxEcRDuOa4IbwK9Q

答案 2 :(得分:0)

我建议您将其转换为类格式,以获得更好的方法以及更高的可读性和可维护性。下面是您的JSON数据的示例类。

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;