• 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
1
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
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

· Add your reply
  • Sort: 
avatar image
8
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
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 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
0

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

112 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

Related Questions

How do I go about deserializing a json array? 2 Answers

Serializing subclass of generic class not working 1 Answer

How to protect JSON file game data? 2 Answers

C# class to JSON 1 Answer

Is it possible to executing object serialization into JSON asynchronously? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges