0

Im busy trying to deserialise the info i receive from google maps API that is in Json format.

I know the json is write and i also used JsonToC# to create the data types but for some reason my classes are not being filled with the json info.

JSON -- https://pastebin.com/gVQ75sfv

C# --

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
using AUP;
using System.Collections.Generic;

//-- John Esslemont

public class rCade_GPS : MonoBehaviour
{
    public Action OnComplete;


    private GPSPlugin gpsPlugin;

    public Text gpsStatusText;
    public Text latitudeText;
    public Text longitudeText;
    public Text speedText;
    public Text altitudeText;
    public Text bearingText;

    // new 
    public Text accuracyText;
    public Text distanceInMetersText;

    //nmea
    public Text timeStampText;
    public Text nmeaText;
    public Text Json;

    private Dispatcher dispatcher;
    public GEO resultFromServer;
    public float Longitude, Latitude;
    public string URL;
    public string FromServer;

    IEnumerator Start()
    {
        OnComplete += Deserialize;
        // First, check if user has location service enabled
        if (!Input.location.isEnabledByUser)
            yield break;

        // Start service before querying location
        Input.location.Start();

        // Wait until service initializes
        int maxWait = 20;
        while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
        {
            yield return new WaitForSeconds(1);
            maxWait--;
        }

        // Service didn't initialize in 20 seconds
        if (maxWait < 1)
        {
            print("Timed out");
            yield break;
        }

        // Connection has failed
        if (Input.location.status == LocationServiceStatus.Failed)
        {
            print("Unable to determine device location");
            yield break;
        }
        else
        {
            // Access granted and location value could be retrieved
            print("Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp);
            Longitude = Input.location.lastData.longitude;
            Latitude = Input.location.lastData.latitude;
        }

        SA_StatusBar.text = Longitude + " ;;;; " + Latitude;
        // Stop service if there is no need to query location updates continuously
        Input.location.Stop();
    }

    public void RequestGEOLocation()
    {
        SA_StatusBar.text = "GPS : Longitude is :: " + Longitude + "Latitude :: " + Latitude;
        GetHumanReadableVersion(Latitude, Longitude, "API KEY");
    }

    public void GetHumanReadableVersion(float longitude, float lat, string apiKey)
    {
#if (!UNITY_EDITOR)
        {
            URL = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + longitude + ',' + lat + "&key=" + apiKey;
        }
#elif(UNITY_EDITOR)
        {
            URL = "https://maps.googleapis.com/maps/api/geocode/json?latlng=55.1301,-1.517228&key=API KEY";
        }
#endif
        SA_StatusBar.text = "Result URL IS " + URL;
        StartCoroutine(WebServiceRequest());
    }

    public virtual IEnumerator WebServiceRequest()
    {
        WWWForm form = new WWWForm();
        WWW wwwRequest = new WWW(URL);

        yield return wwwRequest;

        if (!String.IsNullOrEmpty(wwwRequest.error))
        {
            FromServer = wwwRequest.text;
            Debug.LogError("Error On Page : " + wwwRequest.error);
        }
        else if (wwwRequest.text.Length > 0)
        {
            FromServer = wwwRequest.text;
            if (OnComplete != null)
                OnComplete();
        }
        else
        {
            FromServer = "Null";
            Debug.LogError("We got no responce from the server");
        }
    }

    public void Deserialize()
    {
        Json.text = FromServer;
        resultFromServer = JsonUtility.FromJson<GEO>(FromServer);
        Debug.Log(resultFromServer.results[0]);
    }

    // Create Json encoder to take the long and lat for conversion 
}
[System.Serializable]
public class address_components
{
    public string long_name { get; set; }
    public string short_name { get; set; }
    public List<string> types { get; set; }
}
[System.Serializable]
public class location
{
    public double lat { get; set; }
    public double lng { get; set; }
}
[System.Serializable]
public class northeast
{
    public double lat { get; set; }
    public double lng { get; set; }
}
[System.Serializable]
public class southwest
{
    public double lat { get; set; }
    public double lng { get; set; }
}
[System.Serializable]
public class viewport
{
    public northeast northeast { get; set; }
    public southwest southwest { get; set; }
}
[System.Serializable]
public class northeast2
{
    public double lat { get; set; }
    public double lng { get; set; }
}
[System.Serializable]
public class southwest2
{
    public double lat { get; set; }
    public double lng { get; set; }
}
[System.Serializable]
public class bounds
{
    public northeast2 northeast { get; set; }
    public southwest2 southwest { get; set; }
}
[System.Serializable]
public class geometry
{
    public location location { get; set; }
    public string location_type { get; set; }
    public viewport viewport { get; set; }
    public Bounds bounds { get; set; }
}
[System.Serializable]
public class results
{
    public List<address_components> address_components { get; set; }
    public string formatted_address { get; set; }
    public geometry geometry { get; set; }
    public string place_id { get; set; }
    public List<string> types { get; set; }
}
[System.Serializable]
public class GEO
{
    public List<results> results { get; set; }
    public string status { get; set; }
}
John Esslemont
  • 87
  • 1
  • 3
  • 10
  • Paste your Json [here](http://json2csharp.com/). Generate your classes. Remove `{ get; set; }` from each class. Put `[System.Serializable]` to the top of each class. Use the `RootObject` class to deseralize your json. See the duplicated answer for Unity's API to do this. You can also look there if you run into problems. – Programmer Apr 21 '17 at 16:23
  • Why did it not work with the properties? Also make your comment an answer and i will mark it. – John Esslemont Apr 21 '17 at 16:26
  • 1
    Unity cannot serialize/de-serialize properties. I can't make it an answer because it is a duplicate. Although, you can upvote the duplicated answer if it is useful to you. – Programmer Apr 21 '17 at 16:27

0 Answers0