0
\$\begingroup\$

I have been trying to solve this issue on my own but couldn't find the cause of the issue.
Before I start, I am using unity 5.4 and for creating json data I used JSONObject, https://github.com/mtschoen/JSONObject.

The code i used to send data to server, is below

Debug.Log (jsonObject.ToString ());
byte[] bytes = Encoding.UTF8.GetBytes (jsonObject.ToString());
Debug.Log (Encoding.Default.GetString (bytes));
WWW www = new WWW (url, bytes, headers);
yield return www;
Debug.Log ("Sending Data " + www.text);

I used jsonObject.ToString() just to test sending it as a string. The data is something like

{"Image_name":1,"Links":[{"HotspotName":"User_arch0","Location":"(-1.2, -0.2, 1.6)","Scale":"(0.1, 0.1, 0.1)","LinkedImageID":0,"HotspotName":"User_arch1","Location":"(0.5, 0.5, 1.9)","Scale":"(0.1, 0.1, 0.1)","LinkedImageID":3}]}

The first two log statements give this data correctly into the log in the console. But the last one logging www.text gives this Sending Data

{"Image_name":1,"Links":[{"HotspotName":"User_arch1","Location":"(0.5, 0.5, 1.9)","Scale":"(0.1, 0.1, 0.1)","LinkedImageID":3}]}

As you can see, the values for Hotspot "User_arch0" is just not there in the final output. Even if i send a string directly instead of jsonObject.ToString(), it doesn't work.

What is causing this? If you need anymore of the code let me know

\$\endgroup\$
1
  • \$\begingroup\$ Use tools such as jsonlint.com to validate your hand-made JSON strings! \$\endgroup\$
    – Vaillancourt
    Commented Jan 30, 2017 at 11:30

1 Answer 1

2
\$\begingroup\$

Your JSON is not correct.

If you have 2 links, they should be separated as 2 distinct objects. In your JSON, I see no brackets between the 2 links.

That means it is considered as only 1 object. This object has a HotspotName, which is first assigned to user_arch0 but then overriden to user_arch1, and same goes for all the other attributes, so in your www object in the end you only get second link.

I am not a JSON expert but I guess just adding brackets like this should do it

{"Image_name":1,"Links":[{"HotspotName":"User_arch0","Location":"(-1.2, -0.2, 1.6)","Scale":"(0.1, 0.1, 0.1)","LinkedImageID":0}, {"HotspotName":"User_arch1","Location":"(0.5, 0.5, 1.9)","Scale":"(0.1, 0.1, 0.1)","LinkedImageID":3}]}

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .