Convert JSON String to JSON Object and Parse all data

Hi,

I need to send a nested Json file to a client. The problem is to parse all data properly to recreate the JSON file.

I tried some stuff but I don’t think is the right way.

WebSocket Server

import websockets.*;
import java.io.*;
WebsocketServer ws;

JSONArray vectors;

ArrayList<ArrayList<PVector>> allShapes = new ArrayList();
ArrayList<PVector> shape1 = new ArrayList<PVector>();
ArrayList<PVector> shape2 = new ArrayList<PVector>();
ArrayList<PVector> shape3 = new ArrayList<PVector>();

void setup() {
  size(512, 424, P3D);

  shape1.add(new PVector(40, 20));
  shape1.add(new PVector(20, 40));
  shape1.add(new PVector(60, 40));

  shape2.add(new PVector(140, 120));
  shape2.add(new PVector(220, 420));
  shape2.add(new PVector(60, 410));

  shape3.add(new PVector(10, 20));
  shape3.add(new PVector(300, 220));
  shape3.add(new PVector(40, 340));
  shape3.add(new PVector(92, 20));

  allShapes.add(shape1);
  allShapes.add(shape2);
  allShapes.add(shape3);

  ws = new WebsocketServer(this, 8025, "/socket");
  vectors = new JSONArray();

  for (int i = 0; i < allShapes.size(); i++) {
    JSONObject vector = new JSONObject();
    vector.setString("value", allShapes.get(i).toString());
    vectors.setJSONObject(i, vector);
  }
}

void draw() {

}

void mouseClicked() {
  ws.sendMessage(vectors.toString());
}

void webSocketServerEvent(String msg) {
  println(msg);
}

WebSocket Client

import websockets.*;
import java.io.*;
import java.util.Arrays;
import java.util.List;
import processing.data.*;

WebsocketClient wsc;

void setup() {
  size(200, 200);
  wsc= new WebsocketClient(this, "ws://localhost:8025/socket");
}

void draw() {
}


void mouseClicked() {
  wsc.sendMessage("Client message");
}

void webSocketEvent(String msg) {
  println("************************************");
  JSONArray json = JSONArray.parse(msg);
  for (int i = 0; i < json.size(); i++) {
    JSONObject obj = json.getJSONObject(i);
    String value = obj.getString("value");
    JSONArray v = JSONArray.parse(value);

   

      JSONArray n1 = JSONArray.parse(v.get(0).toString());
      JSONArray n2 = JSONArray.parse(v.get(1).toString());
      JSONArray n3 = JSONArray.parse(v.get(2).toString());

      int[] v1 = n1.getIntArray();
      int[] v2 = n2.getIntArray();
      int[] v3 = n3.getIntArray();

      println(v1[0], v1[1]);
      println(v2[0], v2[1]);
      println(v3[0], v3[1]);

  }
}
1 Like

could you cut down your code to what is your question please?

possibly start with the content of your msg?
put it in a file and skip all the network things.
? is that correct ?

[
  {"value": "[[ 40.0, 20.0, 0.0 ], [ 20.0, 40.0, 0.0 ], [ 60.0, 40.0, 0.0 ]]"},
  {"value": "[[ 140.0, 120.0, 0.0 ], [ 220.0, 420.0, 0.0 ], [ 60.0, 410.0, 0.0 ]]"},
  {"value": "[[ 10.0, 20.0, 0.0 ], [ 300.0, 220.0, 0.0 ], [ 40.0, 340.0, 0.0 ], [ 92.0, 20.0, 0.0 ]]"}
]

and the problem you might have is,
that inside are JSONObjects “value” what are string and not JSONArrays
the " [[],[]]" there should actually be no “”

_ OR ________________

JSONArray vectors, value,segments;
JSONObject vector;
String valuearrays;

String filename="data/vectors.json";

void setup() {
  vectors = loadJSONArray(filename);
  println(vectors);
  for (int i = 0; i < vectors.size(); i++) {
    vector = vectors.getJSONObject(i);
    println("i "+i+"_"+vector);
    valuearrays = vector.getString("value");
    //println(valuearrays);
    value = JSONArray.parse(valuearrays);
    //println(value);   
    for (int j = 0; j < value.size(); j++) {
      segments=value.getJSONArray(j);
      println("i "+i+" j "+j+"_"+segments);
    }
  }
}

1 Like

My question is:

How to send, with WebSocket, neested JSON file, and recreate the ArrayList in the Client.

actually i understood your question like

  • you get a JSON like string over network already
  • but recreating the JSON array structure makes you problems,

and i answer

  • you should repair the sending as
 {"value": "[[ ... ], [ ... ], [ ... ], [ ..]]"}

is not JSON conform

  • OR i manage the read and restore anyway

but the precondition is that that is the string you get?

so please change

void webSocketEvent(String msg) {
  println("************************************");

to

void webSocketEvent(String msg) {
  println(msg);

and show the result.

Does your new question indicate that net transport fails? please provide more details.


now i try also the web sockets, even on 2 computers on my LAN
-a- i found that your code is working,
-b- but the way the arrays are expected is hard coded, so i used my above code
-c- still the fix 3 shape thing is a bad limitation
-d- i included some drawing on server and client side
( come on, we are using processing // need some show )
server on Raspberry Pi / processing 3.5.3


client on win7 / 64bit / processing 3.5.3
using VNC window to have both.

// https://discourse.processing.org/t/convert-json-string-to-json-object-and-parse-all-data/10407
// @KeremTurkyilmaz

/* Websockets 0.1b Lasse Steenbock Vestergaard */
//______________________________________________WebSocket Server
/*
import websockets.*;
 import java.io.*;
 WebsocketServer ws;
 String outmsg;
 
 JSONArray vectors;
 
 ArrayList<ArrayList<PVector>> allShapes = new ArrayList();
 ArrayList<PVector> shape1 = new ArrayList<PVector>();
 ArrayList<PVector> shape2 = new ArrayList<PVector>();
 ArrayList<PVector> shape3 = new ArrayList<PVector>();
 
 PShape shape1D, shape2D, shape3D;
 
 void setup() {
 size(600,600);  //, P3D
 
 shape1.add(new PVector(40, 20));
 shape1.add(new PVector(20, 40));
 shape1.add(new PVector(60, 40));
 
 shape2.add(new PVector(140, 120));
 shape2.add(new PVector(220, 420));
 shape2.add(new PVector(60, 410));
 
 shape3.add(new PVector(10, 20));
 shape3.add(new PVector(300, 220));
 shape3.add(new PVector(40, 340));
 shape3.add(new PVector(92, 20));
 
 allShapes.add(shape1);
 allShapes.add(shape2);
 allShapes.add(shape3);
 
 ws = new WebsocketServer(this, 8025, "/socket");
 vectors = new JSONArray();
 
 for (int i = 0; i < allShapes.size(); i++) {
 JSONObject vector = new JSONObject();
 vector.setString("value", allShapes.get(i).toString());
 vectors.setJSONObject(i, vector);
 }
 outmsg = vectors.toString();
 println(outmsg);
 }
 
 void draw() {
 background(200,200,0);
 noFill();
 stroke(200,0,0);
 beginShape();
 for (PVector spv : shape1 ) { 
 circle(spv.x,spv.y,10);
 vertex(spv.x,spv.y);
 }
 endShape(CLOSE);
 
 stroke(0,200,0);
 beginShape();
 for (PVector spv : shape2 ) {
 circle(spv.x,spv.y,10);
 vertex(spv.x,spv.y);
 }
 endShape(CLOSE);
 stroke(0,0,200);
 beginShape();
 for (PVector spv : shape3 ) {
 circle(spv.x,spv.y,10);
 vertex(spv.x,spv.y);
 }
 endShape(CLOSE);    
 }
 
 void mouseClicked() {  // what does that do?
 ws.sendMessage(outmsg);
 println("send "+outmsg);
 }
 
 void webSocketServerEvent(String inmsg) {
 println(inmsg);
 }
 */

//______________________________________________WebSocket Client

import websockets.*;

String server = "ws://localhost:8025/socket"; // "ws://192.168.1.204:8025/socket"; // 
WebsocketClient wsc;

JSONArray vectors, value, segments;
JSONObject vector;
String valuearrays;

PVector pv = new PVector(0.0, 0.0, 0.0);
ArrayList<PVector> shape1, shape2, shape3;
boolean drawshape = false;

void setup() {
  size(600, 600);
  wsc= new WebsocketClient(this, server);
  println("wait for mouse click on server canvas");
}

void draw() {
  background(200, 200, 0);
  noFill();
  if ( drawshape ) {
    beginShape();
    for (PVector spv : shape1 ) {
      circle(spv.x, spv.y, 10);
      vertex(spv.x, spv.y);
    }
    endShape(CLOSE);
    beginShape();
    for (PVector spv : shape2 ) {
      circle(spv.x, spv.y, 10);
      vertex(spv.x, spv.y);
    }
    endShape(CLOSE);
    beginShape();
    for (PVector spv : shape3 ) {
      circle(spv.x, spv.y, 10);
      vertex(spv.x, spv.y);
    }
    endShape(CLOSE);
  }
}


void mouseClicked() {
  wsc.sendMessage("Client mouse click");
}

void webSocketEvent(String msg) {
  println(msg);
  vectors = JSONArray.parse(msg);
  println("got array records: "+vectors.size());
  for (int i = 0; i < vectors.size(); i++) {
    if ( i == 0 ) shape1 = new ArrayList<PVector>();
    if ( i == 1 ) shape2 = new ArrayList<PVector>();
    if ( i == 2 ) shape3 = new ArrayList<PVector>();
    vector = vectors.getJSONObject(i);
    valuearrays = vector.getString("value");
    value = JSONArray.parse(valuearrays);
    for (int j = 0; j < value.size(); j++) {
      segments=value.getJSONArray(j);
      //println("i "+i+" j "+j+"_"+segments);
      pv.set(segments.getInt(0), segments.getInt(1), segments.getInt(2));  // convert to PVector
      println("i "+i+" j "+j+"_pv_"+pv);
      if ( i == 0 ) shape1.add(new PVector(pv.x,pv.y));
      if ( i == 1 ) shape2.add(new PVector(pv.x,pv.y));
      if ( i == 2 ) shape3.add(new PVector(pv.x,pv.y));
      drawshape = true;
    }
  }
  println(shape1);
  println(shape2);
  println(shape3);
}

1 Like

Thank you very much !!