Search code examples
jsonunity-game-enginelitjson

How to take a particular field from json data in unity


  • Using google reverse geocoding i get a json.
  • i need to access a particular field from it.

i tried the following code

private void Processjson(string jsonString)
{
    JsonData jsonvale = JsonMapper.ToObject(jsonString);

    Debug.Log (jsonvale["status"].ToJson()); // which prints "ok" on console


    parseJSON parsejson;

    string name = jsonvale["results"]["formatted_address"].ToString();
     debug.log(name);
}

here i cant print the name (formatted_address)

my json file is as below

    {
   "results" : [
  {
     "address_components" : [
        {
           "long_name" : "Udumalpet-Poosaripatti-Negamam Road",
           "short_name" : "Udumalpet-Poosaripatti-Negamam Road",
           "types" : [ "route" ]
        },
        {
           "long_name" : "Seelakkampatti",
           "short_name" : "Seelakkampatti",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Coimbatore",
           "short_name" : "Coimbatore",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "Tamil Nadu",
           "short_name" : "TN",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "India",
           "short_name" : "IN",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "642205",
           "short_name" : "642205",
           "types" : [ "postal_code" ]
        }
     ],
     "formatted_address" : "Udumalpet-Poosaripatti-Negamam Road, Seelakkampatti, Tamil Nadu 642205, India",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 10.6607381,
              "lng" : 77.1360873
           },
           "southwest" : {
              "lat" : 10.6576768,
              "lng" : 77.1228984
           }
        },
        "location" : {
           "lat" : 10.6581087,
           "lng" : 77.13010969999999
        },
        "location_type" : "GEOMETRIC_CENTER",
        "viewport" : {
           "northeast" : {
              "lat" : 10.6607381,
              "lng" : 77.1360873
           },
           "southwest" : {
              "lat" : 10.6576768,
              "lng" : 77.1228984
           }
        }
     },
     "place_id" : "ChIJ6SNTGUk0qDsRJs-tBBDJ6Ag",
     "types" : [ "route" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "642205",
           "short_name" : "642205",
           "types" : [ "postal_code" ]
        },
        {
           "long_name" : "Tamil Nadu",
           "short_name" : "TN",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "India",
           "short_name" : "IN",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Tamil Nadu 642205, India",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 10.698639,
              "lng" : 77.26073269999999
           },
           "southwest" : {
              "lat" : 10.6228222,
              "lng" : 77.05489420000001
           }
        },
        "location" : {
           "lat" : 10.6583181,
           "lng" : 77.1830243
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 10.698639,
              "lng" : 77.26073269999999
           },
           "southwest" : {
              "lat" : 10.6228222,
              "lng" : 77.05489420000001
           }
        }
     },
     "place_id" : "ChIJ9en3D_s0qDsRIcoj9NdDJRg",
     "types" : [ "postal_code" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Coimbatore",
           "short_name" : "Coimbatore",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "Tamil Nadu",
           "short_name" : "TN",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "India",
           "short_name" : "IN",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Coimbatore, Tamil Nadu, India",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 11.4057969,
              "lng" : 77.1766741
           },
           "southwest" : {
              "lat" : 10.21881,
              "lng" : 76.656036
           }
        },
        "location" : {
           "lat" : 10.9675289,
           "lng" : 76.91820969999999
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 11.4057969,
              "lng" : 77.1766741
           },
           "southwest" : {
              "lat" : 10.21881,
              "lng" : 76.656036
           }
        }
     },
     "place_id" : "ChIJtRyXL69ZqDsRl3v5ZMPoVUA",
     "types" : [ "administrative_area_level_2", "political" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Tamil Nadu",
           "short_name" : "TN",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "India",
           "short_name" : "IN",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Tamil Nadu, India",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 13.496666,
              "lng" : 80.3464511
           },
           "southwest" : {
              "lat" : 8.0690069,
              "lng" : 76.23055409999999
           }
        },
        "location" : {
           "lat" : 11.1271225,
           "lng" : 78.6568942
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 13.496666,
              "lng" : 80.3464511
           },
           "southwest" : {
              "lat" : 8.0774075,
              "lng" : 76.23055409999999
           }
        }
     },
     "place_id" : "ChIJM5YYsYLFADsR8GEzRsx1lFU",
     "types" : [ "administrative_area_level_1", "political" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "India",
           "short_name" : "IN",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "India",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 35.5087008,
              "lng" : 97.39535869999999
           },
           "southwest" : {
              "lat" : 6.4626999,
              "lng" : 68.1097
           }
        },
        "location" : {
           "lat" : 20.593684,
           "lng" : 78.96288
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 35.5087008,
              "lng" : 97.39498069999999
           },
           "southwest" : {
              "lat" : 6.7535159,
              "lng" : 68.16288519999999
           }
        }
     },
     "place_id" : "ChIJkbeSa_BfYzARphNChaFPjNc",
     "types" : [ "country", "political" ]
  }
   ],
   "status" : "OK"
}

the error when i log the name is as follows

InvalidOperationException: Instance of JsonData is not a dictionary
LitJson.JsonData.EnsureDictionary ()

How can i log the field [formatted_address]


Solution

  • Since it is Unity, we already have JsonUtility which is built-in. I just copied your json string and generated below class from JsonUtils

    public class AddressComponent
    {
        public string long_name;
        public string short_name;
        public List<string> types;
    }
    
    public class Northeast
    {
        public double lat;
        public double lng;
    }
    
    public class Southwest
    {
        public double lat;
        public double lng;
    }
    
    public class Bounds
    {
        public Northeast northeast;
        public Southwest southwest;
    }
    
    public class Location
    {
        public double lat;
        public double lng;
    }
    
    public class Viewport
    {
        public  northeast;
        public  southwest;
    }
    
    public class Geometry
    {
        public Bounds bounds;
        public Location location;
        public string location_type;
        public Viewport viewport;
    }
    
    public class Result
    {
        public List<AddressComponent> address_components;
        public string formatted_address;
        public Geometry geometry;
        public string place_id;
        public List<string> types;
    }
    
    public class Example
    {
        public List<Result> results;
        public string status;
    }
    

    Add above classes in single script and add it into the unity. Now try this code to get the formatted addresses.

    private void Processjson(string jsonString)
    {
        Example example = JsonUtility.FromJson<Example>(jsonString);
    
        Debug.Log (example.results[0].formatted_address);
    }
    

    You can rename the Example class if you want