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 /
avatar image
0
Question by Gilead7 · Mar 15, 2018 at 11:11 PM · c#jsondeserialize

JSON problems and IEnumerator

I'm trying to get JSON data from a server using the WWW class and IEnumerator. I removed all mono behavior inheritance, tried to deserialize the json data to my class, but I end up with an error I've never seen before: ArgumentException: JSON parse error: The document root must not follow by other values. UnityEngine.JsonUtility.FromJson[T] (System.String json) (at C:/buildslave/unity/build/artifacts/generated/common/modules/JSONSerialize/JsonUtilityBindings.gen.cs:25) LoadDatabase+c__Iterator0.MoveNext () (at Assets/Scripts/LoadDatabase.cs:42) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

I'm starting to wonder if the problem is that I'm using a CoRoutine. Could that be the case?

 [System.Serializable]
 public class VehicleIndex 
 {
     public string ID{ get; set;}
     public string Make{ get; set;}
     public string Model{ get; set;}
     public string Year{ get; set;}
     public string Mileage{ get; set;}
 }
 Removed the constructor

 [System.Serializable]
 public class VehicleCollection
 {
 //    public List <VehicleIndex>VehicleList{ get; set;}
     public VehicleIndex[] vehicles;
 }

Created a new class here.

Now the sticky point

 public IEnumerator GetAllVehicles()
     {
         WWW VehicleData = new WWW ("http://localhost/CMVM/LoadVehicle.php"); 
         yield return VehicleData;
 
         Debug.Log (VehicleData.text);
 This part shows all the JSON- all good
 
         string json = VehicleData.text;
 Not quite sure about this..
         var cars = JsonUtility.FromJson<VehicleCollection>(json);
 Can anyone give me a hand with this?

Thanks!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by MacDx · Mar 15, 2018 at 11:37 PM

I think the problem is that your VehicleIndex class is using properties instead of fields. The documentation on JSONUtility never mentions properties. However, it does say that it only supports seralization of what Unity's serialization system does and I'm pretty sure properties are not supported since they don't show up on inspectors by default when you have them on MonoBehaviours.

Quoting the documentation:

Internally, this method uses the Unity serializer; therefore the type you are creating must be supported by the serializer. It must be a plain class/struct marked with the Serializable attribute. Fields of the object must have types supported by the serializer. Fields that have unsupported types, as well as private fields or fields marked with the NonSerialized attribute, will be ignored.

So I can think of 2 possible solutions to this problem.

1) This post on the forum says that you should try putting the [SerializeField] attribute on top of each of your properties. https://forum.unity.com/threads/jsonutility-and-properties.382526/

2) Don't use properties, use public fields instead. All of your properties are auto-implemented anyways (default get and set) and it doesn't look like you're gonna be using reflection or have virtual properties that will be overridden by child classes, so there wouldn't be any difference if you swapped them for fields. (This option is better IMO)

Hope this helps!

Edit: A third option would be to use a different JSON serialization library like Json.NET that does support property serialization by default.

Comment
Add comment · Show 6 · 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 Gilead7 · Mar 20, 2018 at 04:21 PM 0
Share

Thanks $$anonymous$$axDx! Looks like I have more problems. I chose the third option and installed Newtonsoft Json for Unity. After deserializing, it gave the error

 JsonReaderException: Additional text encountered after finished reading JSON content: {. Path '', line 1, position 87.
 Newtonsoft.Json.JsonTextReader.Read () (at C:/Project/Github/Json.Net.Unity3D/src/Newtonsoft.Json/JsonTextReader.cs:408)
 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) (at C:/Project/Github/Json.Net.Unity3D/src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs:193)
 Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) (at C:/Project/Github/Json.Net.Unity3D/src/Newtonsoft.Json/JsonSerializer.cs:823)
 Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) (at C:/Project/Github/Json.Net.Unity3D/src/Newtonsoft.Json/JsonSerializer.cs:802)
 Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) (at C:/Project/Github/Json.Net.Unity3D/src/Newtonsoft.Json/JsonConvert.cs:865)
 Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) (at C:/Project/Github/Json.Net.Unity3D/src/Newtonsoft.Json/JsonConvert.cs:822)
 Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value) (at C:/Project/Github/Json.Net.Unity3D/src/Newtonsoft.Json/JsonConvert.cs:759)
 LoadDatabase+<GetAllVehicles>c__Iterator0.$$anonymous$$oveNext () (at Assets/Scripts/LoadDatabase.cs:44)
 UnityEngine.SetupCoroutine.Invoke$$anonymous$$oveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
 

Couldn't find anything wrong with the json output from the WWW class.

{"ID":"1999SentraGXE","$$anonymous$$ake":"Nissan","$$anonymous$$odel":"Sentra","Year":"1999","$$anonymous$$ileage":"36000"}{"ID":"FAFP53U17A218749","$$anonymous$$ake":"Ford","$$anonymous$$odel":"SemiPanel","Year":"2015","$$anonymous$$ileage":"12"}{"ID":"G4G$$anonymous$$5EX1E9324742","$$anonymous$$ake":"$$anonymous$$ack","$$anonymous$$odel":"Dumpster","Year":"2012","$$anonymous$$ileage":"120"} UnityEngine.Debug:Log(Object) c__Iterator0:$$anonymous$$oveNext() (at Assets/Scripts/LoadDatabase.cs:37) UnityEngine.SetupCoroutine:Invoke$$anonymous$$oveNext(IEnumerator, IntPtr) This is the debug.log result.

So I went to see if the json was valid, but the JOSN Formatter said there were too many JSON root elements. Not sure how to fix it. Here is the PHP:

 <?PHP
 include('myconfig.php');
  $resource = $connection->query('SELECT * FRO$$anonymous$$ vehicle');
 while ( $rows = $resource->fetch_assoc() ) 
 {
     print json_encode($rows);
      
 
 }

Thanks!

avatar image Bunny83 Gilead7 · Mar 20, 2018 at 04:40 PM 1
Share

This is not valid json text:

 {"ID":"1999SentraGXE","$$anonymous$$ake":"Nissan","$$anonymous$$odel":"Sentra","Year":"1999","$$anonymous$$ileage":"36000"}
 {"ID":"FAFP53U17A218749","$$anonymous$$ake":"Ford","$$anonymous$$odel":"SemiPanel","Year":"2015","$$anonymous$$ileage":"12"}
 {"ID":"G4G$$anonymous$$5EX1E9324742","$$anonymous$$ake":"$$anonymous$$ack","$$anonymous$$odel":"Dumpster","Year":"2012","$$anonymous$$ileage":"120"} 

As those are three seperate objects without any structural connection. You can only have one "top level" Json value. Since you have several objects you may want to wrap them in an array.

avatar image Gilead7 · Mar 20, 2018 at 05:40 PM 0
Share

Thanks Bunny. What can I do to the php, it's already using fetch_array, to make the structure sound? Or do I need to do it Unity-side?

avatar image Bunny83 Gilead7 · Mar 20, 2018 at 06:31 PM 0
Share

I'm not that familiar with PHP but something like this should work i guess:

 $resource = $connection->query('SELECT * FRO$$anonymous$$ vehicle');
 $cars = array();
 while ( $rows = $resource->fetch_assoc() ) 
 {
     $cars[] =  $rows;
 }
 $data = array("vehicles"=>$cars);
 print json_encode($data);

This should output something like this:

 {
     "vehicles":[
         {
             "ID":"1999SentraGXE",
             "$$anonymous$$ake":"Nissan",
             "$$anonymous$$odel":"Sentra",
             "Year":"1999",
             "$$anonymous$$ileage":"36000"
         },
         {
             "ID":"FAFP53U17A218749",
             "$$anonymous$$ake":"Ford",
             "$$anonymous$$odel":"SemiPanel",
             "Year":"2015",
             "$$anonymous$$ileage":"12"
         },
         {
             "ID":"G4G$$anonymous$$5EX1E9324742",
             "$$anonymous$$ake":"$$anonymous$$ack",
             "$$anonymous$$odel":"Dumpster",
             "Year":"2012",
             "$$anonymous$$ileage":"120"
         } 
     ]
 }


So it's one object (your "VehicleCollection" object) with just one field: "vehicles". This field is an array or your car objects

avatar image Gilead7 Bunny83 · Mar 21, 2018 at 03:50 PM 0
Share

It worked! The JSON is now valid. Thanks again!

avatar image Gilead7 · Mar 20, 2018 at 07:01 PM 0
Share

Thanks Bunny!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

463 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 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 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 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 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

decode json response 1 Answer

JSON vs XML for Unity C# 1 Answer

Making a bubble level (not a game but work tool) 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