Wednesday, 5 June 2019

Unity C# Parsing JSON data using SimpleJSON


1. Download the SimpleJSON .cs from this link and https://github.com/Bunny83/SimpleJSON
and create a folder called "plugin" under unity project assets folder and place the SimpleJSON.cs script inside.

2. Create a new class called as SampleJSONParsing

3. Paste the below code on to the newly created class.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SimpleJSON;


public class SampleJSONParsing :MonoBehaviour
{

    string encodedString = "{\"method\": \"retrieve_configuration\",\"params\": {\"configuration\" :\"109873839\"}}";

    void Start()
    {
        JSONNode jsonNode = SimpleJSON.JSON.Parse(encodedString);
        string action = jsonNode["params"][0].Value;
        Debug.Log(action);
    }


}

4. create a new scene attach the script to an empty game object and run the scene.

5. Similarly to parse your data pass it as a string.

No comments:

Post a Comment