• 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
Question by STBarnard · Feb 29, 2016 at 12:39 PM · c#serializationjson

JsonUtility and Arrays [Error - "JSON must represent an object type"]

Hi all,

I know this has been asked previously and I have looked through those post. I am having a hard time wrapping my brain around what I have to do to get an array to work with the JsonUtitlity Serialization, or if it's even possible.

Heres what I have:

 [System.Serializable]
     public class JSONObject : System.Object  {
         public string[] values;
     }
 
     public void request (params string[] values){
         JSONObject JSON = new JSONObject ();
         JSON.values = values;
         StartCoroutine(webCoro(JsonUtility.ToJson(JSON)));
     }
 
     void response (string JSONstring) {
         JSONObject JSON = JsonUtility.FromJson <JSONObject> (JSONstring);
     }
     
     IEnumerator webCoro (string data){
         string URL = "http://" + host + ":" + port;
         WWW request = new WWW (URL, encoding.GetBytes (data));
         while (!request.isDone) {
             yield return null;
         }
         Debug.Log ("Request Error: " + request.error);
         response (request.text);
     }
 
     public void test (){
         request ("script", "action", "test"); 
     }

When I run test () I get:

ArgumentException: JSON must represent an object type. UnityEngine.JsonUtility.FromJson[JSONObject] (System.String json) (at C:/buildslave/unity/build/artifacts/generated/common/modules/JSONSerialize/JsonUtilityBindings.gen.cs:24) Web.response (System.String JSONstring) (at Assets/Web.cs:36) Web+c__Iterator0.MoveNext () (at Assets/Web.cs:50)

What do I need to do to make this happen?

Thanks in advance, Sean

Comment
Bunny83

People who like this

1 Show 0
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

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Bunny83 · Feb 29, 2016 at 01:02 PM

Well, how does your JSON data looks like? The deserializer expects something like this:

 { "values":["one","two","three"]}

How does your data look like? Is it just an array? The JsonUtility seems to only work with objects as root element. If your data looks like this:

 ["one","two","three"]

You might want to wrap it manually in an object like this:

 string yourJSONArray;
 string JSONToParse = "{\"values\":" + yourJSONArray + "}";
Comment
STBarnard
hubatish
hmathiy
kryzodoze
GuruJeya14
DeathPro
ensomniac
Abhinav16

People who like this

8 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 STBarnard · Feb 29, 2016 at 04:23 PM 0
Share

So ultimately the answer was that my node.js code was using JSON.stringify (myRequest) on a request that was already in JSON format giving me :

"{\"values\":[\"script\",\"action\",\"test\"]}"

So when I cut that out I received a response more to the tune of:

{"values":["script","action","test"]}

And this worked out, thanks everyone for your responses, they helped me come to this conclusion.

Sean

avatar image kryzodoze · Sep 17, 2017 at 04:04 AM 0
Share

Thank you Bunny83, this is exactly what I needed.

avatar image

Answer by STBarnard · Feb 29, 2016 at 01:11 PM

Had my node server echo some data back.

"{\"values\":[\"script\",\"action\",\"test\"]}"

That's what i'm working with.

Comment

People who like this

0 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 flashframe · Feb 29, 2016 at 02:27 PM 1
Share

I just tested your code without the WWW request, and it seems to serialise the array fine.

The string should look like this: {"values”:[“script”,”action”,”test”]}

Did you see this answer? http://answers.unity3d.com/questions/844423/wwwtext-not-reading-utf-8-text.html

avatar image STBarnard flashframe · Feb 29, 2016 at 04:15 PM 0
Share

I tried this based off that post:

 IEnumerator webCoro (string data){
         string URL = "http://" + host + ":" + port;
         WWW request = new WWW (URL, encoding.GetBytes (data));
         while (!request.isDone) {
             yield return null;
         }
         Debug.Log ("Request Error: " + request.error);
         string responseString = System.Text.Encoding.UTF8.GetString(request.bytes, 3, request.bytes.Length - 3);
         Debug.Log (responseString);
         response (responseString);
     }

It just cut off the opening brackets:

"values\":[\"script\",\"action\",\"test\"]}"

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

111 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

Related Questions

JsonUtility array not supported? 7 Answers

How to serialize parameters from JSON? 1 Answer

JsonUtility doesn't serialize nested mixed var 1 Answer

JsonUtility serializes floats with way too many digits 2 Answers

Multiple Cars not working 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