- Home /
JsonUtility.FromJson cannot convert object from rest call
Dear Unity,
I have a rest call which implements sensor data and creates an object from it. For some reason it won't parse my json into an object with JsonUtility.FromJson even though i have a valid json:
The error is:
system.ArgumentException' in UnityEngineProxy.dll
ArgumentException: JSON must represent an object type.
Which of course means that it must but a correct object type but as far as i can see it is correct.
My json which i recieve with www.downloadHandler.text would be:
"{\"guid\":\"2a005e1e-a417-43d9-812c-4cbec6e70847\",\"deviceName\":\"Bmp280Sensor\",\"level\":\"critical\"}"
with the object :
[System.Serializable]
public class Bmp280 {
public Guid guid;
public string deviceName;
public string level;
}
and it's being called like this:
private Bmp280 TextToJsonBMP280Object(string getMessage)
{
System.Diagnostics.Debug.WriteLine("The message is: " + getMessage);
return JsonUtility.FromJson<Bmp280>(getMessage);
}
Does anyone see what I am doing wrong? As far as i can see it's exactly what is requested from the function...
Answer by gameplay4all · Mar 21, 2017 at 01:36 PM
I think it has something to do with the backslash before every quotation mark, or is this formatting because of this forum?
The JsonUtility makes JSON that looks like this:
{"customColor":{"r":100,"g":180,"b":120,"a":255},"myBool":true}
So no backslashes :) Hope this was your problem,
-Gameplay4all
Yes, if the actual text contains escaped quotation marks it won't work. If it in deed contains an escaped string for some reason you have to unescape it.
Wow that is probably it. Will try this tommorow and will make you both as answer if that is the answer!
Ok could not resist tried it anyways.
It seems to work and it gives me the following message now: "{"guid":"78002716-cfb5-417c-9777-8bee4ee9d94a","deviceName":"Bmp280Sensor","tempSensorValue":24.2481155,"pressureSensorValue":101041.328,"altitudeSensorValue":23.64571,"measurementTime":"2017-03-21T07:53:43.9098904-07:00","measurename":"LightSensor","level":"critical"}"
Thid does bring me to the next error : JSON parse error: The document root must not follow by other values.
But i think this comes down to something else but that wouldn't fit in this topic. Thanks all :)
Your answer
Follow this Question
Related Questions
Upload/store string to html/json script?? 0 Answers
iPhone bad URL 0 Answers
WWW data acquisition from remote server failing 2 Answers
Using MiniJSON 1 Answer