0

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 !

Flowzer
  • 3
  • 4

1 Answers1

0

Your json data format is invalid. You can do Substring

yourstring.Substring (int startindex, int length_of_string);

and take string starting from { and ending with }, then you will be able to parse to json.