Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

SimpleJSON - Lightweight and easy to use JSON importer / exporter

Discussion in 'Assets and Asset Store' started by Orbcreation, Nov 26, 2014.

  1. Orbcreation

    Orbcreation

    Joined:
    May 4, 2010
    Posts:
    231
    For my own project I use a webserver with a database behind it and let my users communicate with it through JSON, because it is lightweight and very readable.

    I needed a lightweight importer / exporter, that uses only 1 line of code to activate and produces a plain and simple structure of Hashtables and ArrayLists. And I wanted it to be a bit forgiving in case of JSOn format errors as well. I hate it when an importer throws an exception the very second you forgot to add a comma at the end.

    So I made the importer and added some handy functions to the exisiting Hashtable and ArrayList classes, to easily retrieve information.

    Import a JSON string:
    myHashtable = SimpleJsonImporter.Import(jsonString);

    Export a JSON string:
    string jsonString = myHashtable.JsonString();

    Query the hierarchy
    Hashtable bookInfo = myHashtable.GetNodeWithProperty("Author", "George Orwell");

    All source code is included and written in C#. There are no DLL's or hidden stuff.

    View the online webplayer demo (also included in the package) here
    The documentation can be viewed on my website.


     
  2. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    HI, I have bought this and although I can get it to download json from a URL I cannot get it to tell me if a given key exists, all i seem to see in the docs is being able to find a given key with a given value?

    I need to be able to determine if the json contains a key name 'error'

    Help please?

    Thanks
     
  3. Orbcreation

    Orbcreation

    Joined:
    May 4, 2010
    Posts:
    231
    You have several options:
    Code (CSharp):
    1. Hashtable myJsonHash = SimpleJsonImporter.Import(myJsonString);
    2.  
    3. if(myJsonHash == null) Debug.LogError("something went terribly wrong");
    4. else {
    5.     // depending on how your json is set up, you can do one of the following:
    6.  
    7.     // When error is a direct property of your json Hashtable, this is the easiest way
    8.     if(myJsonHash.ContainsKey("error")) {
    9.         Debug.LogError((string)myJsonHash["error"]);
    10.     }
    11.  
    12.     // Alternative
    13.     object node = myJsonHash.GetNodeAtPath(new string[1] {"error"});
    14.     if(node != null) {
    15.         Debug.LogError((string)node);
    16.     }
    17.  
    18.     // When the error is deeper down the json structure
    19.     object deeperNode = myJsonHash.GetNodeAtPath(new string[3] {"data", "queryResults", "error"});
    20.     if(deeperNode != null) {
    21.         Debug.LogError((string)deeperNode);
    22.     }
    23. }
    24.  
     
    elpuerco63 likes this.
  4. momologon

    momologon

    Joined:
    Jul 18, 2015
    Posts:
    1
    I have bought the plugin and understand the simple operation. However, "GetNodeAtPath" can only return the first object, how can I get the others ? For example { type : feature [ data ], type : feature [data], type : feature [data], .... }
     
  5. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    Thanks ;-)
     
  6. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
    What's i must do from here ? please

    DirectoryInfo levelDirectoryPath = new DirectoryInfo(dir);
    FileInfo[] fileInfo = levelDirectoryPath.GetFiles("*.*", SearchOption.AllDirectories);
    foreach (FileInfo file in fileInfo) {

    // file extension check
    if (file.Extension == ".json") {
     
  7. Orbcreation

    Orbcreation

    Joined:
    May 4, 2010
    Posts:
    231
    Code (CSharp):
    1.  
    2. DirectoryInfo levelDirectoryPath = new DirectoryInfo(dir);
    3. FileInfo[] fileInfo = levelDirectoryPath.GetFiles("*.json", SearchOption.AllDirectories);
    4. foreach (FileInfo file in fileInfo) {
    5.    StreamReader streamReader = new StreamReader(file.ToString());
    6.   string fileContents = streamReader.ReadToEnd();
    7.   Hashtable myJsonHash = SimpleJsonImporter.Import(fileContents);
    8.   // Do something with your json stuff here
    9.  
    10.    streamReader.Close();
    11. }
    12.  
    Or something like that