0

I'm having troubles trying to parse a json into a string array in unity. It works just fine, but when I receive decimals it doesn't work.

My response class:

public class Response
{
    public string[] data;
    public string key;
    public string hash;
    public string error;
    public string callback;
}

And my json

{
"data":[11,0,962609.4], 
"key":"5a1d521ebaac844bf98a78689f4acdf8",
"hash":"9ed52bfcc53e823511f1fccad862900b",
"error":0,
"callback":""
}

The problem occurs when parsing data, it just stays null.

This json works perfectly:

{
"data":[11,0,962609], 
"key":"5a1d521ebaac844bf98a78689f4acdf8",
"hash":"9ed52bfcc53e823511f1fccad862900b",
"error":0,
"callback":""
}

This is how I parse the json

var response = JsonUtility.FromJson<Response>(json);
HandleResponse(response, callback);

UPDATE: I realized that it happens with another examples like:

{
"data":[0,0,0],
"key":"6d8dfe0fc69adb1b9eec886fc2385392",
"hash":"52e388c48d705e4351415aa5f9237150",
"error":2,
"callback":""
}

Now I'm really lost.

Thanks you guys.

TomServo
  • 7,248
  • 5
  • 30
  • 47
Maxi
  • 25
  • 1
  • 5
  • Looks like `public string[] data;` is wrong and should be `public double[] data;` (if floating point values are required). `error` also looks like it should be an `int`? – crashmstr Feb 23 '18 at 19:17
  • Why are you defining a value type and then using another?? Like, `string error;` means that `error` should be a string (ie; text) but then, you JSON has numbers `"error": 0`. The same with `string[] data`. If you tell to the code to expect one kind of value why are you using another? – mayo Feb 23 '18 at 19:31
  • I tried this and it wotks perfect, the problem is that data may contain strings. I also detect the problem with null, it was my fault. Though, now I have the problem that, when parsing a decimal number it adds a lot of ceros – Maxi Feb 23 '18 at 19:39
  • I am closing because the json is not invalid with the class you are trying to de-serialize into. This applies to your `data` and `error` variables. Your other questions shows that you are trying to create json by hand. Do not do this unless you study and understand json syntax. You don't at this moment. Create a class then use `JsonUtility.ToJson` to create a json for your self. `JsonUtility.ToJson` will always generate the correct json for your object. – Programmer Feb 23 '18 at 20:26

0 Answers0