Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Aug 04, 2016 at 11:28 AM by kts1021 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by kts1021 · Jul 21, 2016 at 10:37 AM · unity 5wwwjson

JsonUtility returns {}

I have this JSON fetched from web service:

 {"city":{"cityName":"*****","distance":0,"latitude":*****,"cityId":3,"longitude":*****}}

this works fine in web browsers but if I run my code in unity, unity just displays { }.

 class Coordinate
 {
     public string cityName;
     public string distance;
     public string latitude;
     public string cityId;
     public string longitude;
 }

 NameValueCollection values = new NameValueCollection();
     values.Add("latitude", latitude.ToString());
     values.Add("longitude", longitude.ToString());
     values.Add("radius_in_km", "0.5");

     Debug.Log("Latitude: " + values.Get("latitude") + ", Longitude: " + values.Get("longitude") + ", radius_in_km: " + values.Get("radius_in_km"));
     
     string url = "";

     WWW www = new WWW(url);
     Debug.Log("Requesting the following url: " + www.url);
     yield return www;

     Coordinate json = JsonUtility.FromJson<Coordinate>(www.text);

     if(www.error == null)
     {
         Debug.Log(json.cityId);
         Debug.Log("Loaded following JSON string " + JsonUtility.ToJson(www.text));
     }
     else
     {
         Debug.Log("ERROR: " + www.error);
     }

The code just gives me { }. Am I missing something? I am fairly new to JSON and Unity

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by superpig · Jul 27, 2016 at 01:36 AM

As @UnrealSoftware says, your type definition doesn't quite match your JSON - you're trying to read a Coordinate, but your JSON actually describes a bigger object which contains a Coordinate as the "city" field.

So, you need to define a second class, like this:

 [Serializable]
 class CityInfo
 {
    public Coordinate city;
 }

and use FromJson with that. (Note that you should also put the [Serializable] attribute on your Coordinate class).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by UnrealSoftware · Jul 21, 2016 at 02:14 PM

I'm not familiar with Unity's JSON functions and I'm not sure if these are the actual problems but I noticed two things:

  • Your class is called "Coordinate" but in the JSON the same thing is called "city"

  • Some values in the JSON seem to be integers or floats (the ones without quotes around the values) but in your class all values are strings.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kts1021 · Jul 23, 2016 at 11:48 AM 0
Share

Uhmm.. I never used JSON before. So.. if I change Coordinates to City and change the values to appropriate data types will it amend the issue?

avatar image UnrealSoftware kts1021 · Jul 23, 2016 at 11:53 AM 0
Share

I only used other JSON libraries, not the one from Unity. For other libraries it's crucial to get the data types and names right. It shouldn't be hard to test this so I guess you should give it a try.

Follow this Question

Answers Answers and Comments

99 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Differentiate movie and audio file on game object 1 Answer

How to parse googlesheet json 0 Answers

Update url constantly to show updated data? 0 Answers

Json Utilities returns empty object (javascript) 2 Answers

How To Deserialize Json Data Into List? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges