Search Unity

[JsonFX] WTF? Illegal JSON sequence?

Discussion in 'Editor & General Support' started by DaveMatney, Jul 10, 2012.

  1. DaveMatney

    DaveMatney

    Joined:
    Jul 3, 2012
    Posts:
    5
    So, I'm converting a project from XML to JSON, and I've opted to go the JsonFX route (it's documentation made the most sense to me, and it at least SOMETIMES works on iOS and Android, which I'll want it to do in the future), using the build by darktable.

    To get familiar with JsonFX, I've been working inside the JsonFXDemo project, seeing how that particular scene works and trying to duplicate it using my own data.

    So... here's my json file (CartLocations.txt)
    Code (csharp):
    1. {
    2.       "item":[
    3.             {
    4.                 "number":1,
    5.                 "friendlyName":"A Yellow Box",
    6.                 "fileName":"Yellow_Box",
    7.                 "drawer":1,
    8.                 "x_location":0,
    9.                 "y_location":0
    10.             },
    11.             {
    12.                 "number":2,
    13.                 "friendlyName":"A Black Box",
    14.                 "fileName":"Black_Box",
    15.                 "drawer":5,
    16.                 "x_location":0,
    17.                 "y_location":0.8
    18.             },
    19.             {
    20.                 "number":3,
    21.                 "friendlyName":"A Blue Box",
    22.                 "fileName":'"Blue_Box",
    23.                "drawer":2,
    24.                "x_location":0,
    25.                "y_location":0.6
    26.            },
    27.            {
    28.                "number":4,
    29.                "friendlyName:'A Red Box',
    30.                "fileName:'Red_Box',
    31.                "drawer:6,
    32.                "x_location:0,
    33.                "y_location:0.4
    34.            },
    35.            {
    36.                "number":5,
    37.                "friendlyName":"A White Box",
    38.                "fileName":'"White_Box",
    39.                 "drawer":3,
    40.                 "x_location":0,
    41.                 "y_location":0.2
    42.             }
    43.         ]
    44.    
    45. }
    Here's my C# code:
    Code (csharp):
    1. using UnityEngine;
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.IO;
    6. using DB = UnityEngine.Debug;
    7. using DC = DebugConsole;
    8. using JsonFx.Json;
    9.  
    10. public class Item {
    11.     public long number;
    12.     public string friendlyName;
    13.     public string fileName;
    14.     public long drawer;
    15.     public float x_location;
    16.     public float y_location;
    17. }
    18.  
    19. public class ItemSearch {
    20.     public Item[] results;
    21. }
    22.  
    23. public class MX_JsonFXDemo : MonoBehaviour {
    24.    
    25.     public string fileName;
    26.    
    27.     void Start () {
    28.         DC.IsOpen = true;
    29.        
    30.         fileName = "Assets/Resources/CartLocations.txt";
    31.        
    32.         LoadTable();
    33.     }
    34.    
    35.     void LoadTable(){
    36.         if (!File.Exists(fileName)) {
    37.             DB.Log (DC.Log("File does not exist"));
    38.         }
    39.         using (StreamReader sr = File.OpenText (fileName)){
    40.             String input = sr.ReadToEnd();
    41.             var rawData = JsonReader.Deserialize<ItemSearch>(input);    //This is line 41, where the error happens
    42.            
    43.             foreach (var thing in rawData.results) {
    44.                 DB.Log (DC.Log (thing.friendlyName));  
    45.             }
    46.            
    47.         }
    48.     }
    49.  
    50. }
    And here's the error I'm getting:
    Code (csharp):
    1. JsonDeserializationException: Illegal JSON sequence.
    2. JsonFx.Json.JsonReader.Tokenize (Boolean allowUnquotedString)
    3. JsonFx.Json.JsonReader.Tokenize ()
    4. JsonFx.Json.JsonReader.ReadObject (System.Type objectType)
    5. JsonFx.Json.JsonReader.Read (System.Type expectedType, Boolean typeIsHint)
    6. JsonFx.Json.JsonReader.ReadArray (System.Type arrayType)
    7. JsonFx.Json.JsonReader.Read (System.Type expectedType, Boolean typeIsHint)
    8. JsonFx.Json.JsonReader.ReadObject (System.Type objectType)
    9. JsonFx.Json.JsonReader.Read (System.Type expectedType, Boolean typeIsHint)
    10. JsonFx.Json.JsonReader.Deserialize (Int32 start, System.Type type)
    11. JsonFx.Json.JsonReader.Deserialize (System.String value, Int32 start, System.Type type)
    12. JsonFx.Json.JsonReader.Deserialize[ItemSearch] (System.String value)
    13. MX_JsonFXDemo.LoadTable () (at Assets/MX_JsonFXDemo.cs:41)
    14. MX_JsonFXDemo.Start () (at Assets/MX_JsonFXDemo.cs:32)
    15.  
    Because I haven't asked it to do anything beyond deserialize the raw data, and that's where the error is being thrown, I'm lost. It looks incredibly similar to the JsonFXDemo.cs that came with DarkTable's Unity Project, minus a few things that separate the data he streams (a twitter search, syntax here).

    Any ideas?
     
  2. Jaimi

    Jaimi

    Joined:
    Jan 10, 2009
    Posts:
    6,208
    WTF? <g>

    Your error is here:

    "fileName": '"Blue_Box",

    Notice the extra quote.

    You can validate your data here: http://jsonlint.com/
     
  3. mckamey

    mckamey

    Joined:
    Dec 13, 2010
    Posts:
    10
    The error is quite correct. Your input is full of invalid JSON, namely sloppy quotes all over the place. Like Jaimi said, use jsonlint.com if you are unsure of where the syntax issues are. You'll find the Blue_Box line is just the start...
     
  4. DaveMatney

    DaveMatney

    Joined:
    Jul 3, 2012
    Posts:
    5
    Well, I feel sheepish. That's what I get for manually throwing the quotes in instead of Find/Replacing them all.

    Thank you. :)

    Now I get to deal with the issue of my code still not pulling anything. :(
     
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    thats cause ItemSearch has a field named results, but in your json its called item

    it will look in the json for results -> does not find it -> returns an empty one
     
  6. DaveMatney

    DaveMatney

    Joined:
    Jul 3, 2012
    Posts:
    5
    Thank you!