Parse JSON Data from server

Hi !

I know that there are many thread talking about parsing json data but the json data that I get from my server is not common.

So I have a c# script in unity that get json data from my node.js server using websocket.

This my code in unity :

using UnityEngine;
using System.Collections;
using System;
using System.Threading;
using WebSocketSharp;


[Serializable]
public class ParamJSON
{
    public string parameter;
    public string unit;
    public int count;
    public float value;
    public Time time;
}

public class Program : MonoBehaviour
{
    string waka;
    ParamJSON data;
    WebSocket ws;

    // Use this for initialization
    void Start()
    {
        //ws = new WebSocket(url of server);
        print("Open socket: " + ws.ReadyState);
        print("Websocket Alive: " + ws.IsAlive);

      
         ws.OnMessage += (sender, e) =>
        {
            waka = e.Data;
            print(waka);
            //ParamJSON[] obj = JsonHelper.getJsonArray<ParamJSON>(waka);
            //print(obj);

            //ParamJSON P = JsonUtility.FromJson<ParamJSON>(waka);
            //Debug.Log(P.parameter);

            //data = CreateFromJSON(waka);
            //Debug.Log(data);
            //Debug.Log("parameter = " + data.parameter);

            //print("JSON data : " + CreateFromJSON(waka));

        };



        ws.OnOpen += (sender, e) => {
            print("WebSocket-> Open:");
            print("Open socket-> OnOpen: " + ws.ReadyState);
        };


        ws.OnError += (sender, e) => {
            print("WebSocket-> Error: " + e.Message);
            print("Open socket-> OnError: " + ws.ReadyState);
        };


        ws.OnClose += (sender, e) => {
            print("WebSocket-> Close-code: " + e.Code);
            print("WebSocket-> Close-reason: " + e.Reason);
            print("Open socket-> OnClose: " + ws.ReadyState);
        };

        ws.Connect();
    }


    public class JsonHelper
    {
        public static T[] getJsonArray<T>(string json)
        {
            string newJson = "{ \"array\": " + json + "}";
            Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(newJson);
            return wrapper.array;
        }

        [Serializable]
        private class Wrapper<T>
        {
            public T[] array;
        }
    }

    //public static ParamJSON CreateFromJSON(string jsonString)
    //{
    //    return JsonUtility.FromJson<ParamJSON>(jsonString);
    //}



    // Update is called once per frame
    void Update()
    {

    }


    void OnDestroy()
    {
        if (ws != null && ws.ReadyState == WebSocketState.Open)
            ws.Close();
    }

}

The output from the console is : 42[“P0”,“{"unit":"hPA","time":"11:18:06.836736","count":7,"parameter":"P0","value":1021.799988}”]

So I woud like to know how can I parse this json data ?
If it’s possible to have something like (in my console):
unit: hPA
time: 11:18:06.836736
count: 7
etc…

All the line in comment is all that I have already tried to do.

Thank you !

If what you pasted is exactly what is coming back from your server. This is what you’d have to do.

// The leading number "42" is not valid JSON. So that needs to be removed.
string result = e.Data.Substring(2, e.Data.Length - 2);

// The remaining JSON is a string[], so parse that.
string[] array = JsonUtility.FromJson<string[]>(result);

// Your data is then stored in the second value and has escaped characters.
string dataClass = System.Text.RegularExpressions.Regex.Unescape(array[1]);

// You can now deserialize your class
ParamJSON data = JsonUtility.FromJson<ParamJSON>(dataClass);

So, this isn’t valid json, which you know already. But there are a few things that might work to convert it to valid json.
First, everything between the { and } will create a valid json, however, you also need to replace all the \ out of the string. You could replace the \ with an empty string which will remove them.

Once you get that done, you can cast it into a class using whatever json tool you have.

You can validate your json string here https://jsonlint.com/ if you are unsure it’s a valid string or not.