I am trying to access the Places API from Google maps. I already am retrieving a JSON object but as it is a large object with many different objects, how can I access each different object or just a few objects inside it?
I am not sure how should I create the class to be populated by the JSON object
Thats how I am doing it:
using UnityEngine;
using System.Net;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;
[System.Serializable]
public class Places
{
public object[] results;
public string icon;
public string name;
}
public class GooglePlaces : MonoBehaviour {
private string placesApiKey = "YOURKEYHERE";
void Start () {
StartCoroutine(GetText());
}
IEnumerator GetText() {
string placesURL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&type=restaurant&keyword=cruise&key="+placesApiKey;
using(UnityWebRequest www = UnityWebRequest.Get(placesURL)) {
yield return www.Send();
if(www.isError) {
Debug.Log(www.error);
}
else {
object googleRestaurant = www.downloadHandler.text;
string jsonString = googleRestaurant.ToString();
Debug.Log ("JSON string:"+googleRestaurant.ToString ());
Debug.Log ("JSONUtility result: "+JsonUtility.FromJson<Places>(jsonString));
}
}
}
}