Convert JSON String to JSON Object and Parse all data

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