#java.org.JSONObject returning the bytesArray

20 messages · Page 1 of 1 (latest)

maiden seal
#

What is data?

inner jewel
#
{
    "string": "A",
    "number": 1,
    "boolean": true,
    "null": null,
    "list": [1,true,"done"],
    "dict": {"done":true}
}
slow quartz
#

prints just fine for me are you sure that data actually is the json?

#

try to see json in a debugger

inner jewel
#

i using a Skript-reflec its a minecraft plugin fro running java inside file.

import:
   org.json.JSONObject as jsonObject


  set {_payload} to new String("{'string':'A','number':1,'boolean':true,'null':null,'list':[1,true,'done'],'dict':{'done':true}}")
  replace all "'" in {_payload} with """" 
  # need to be doubled "" bcs that is escape = '""' = '"'
  set {_a} to new jsonObject({_payload})
  broadcast {_a}

  broadcast "&cDebug with GSON"
  set {_gs} to new GsonBuilder().setPrettyPrinting().create()
  set {_jp} to new JsonParser()
  set {_je} to {_jp}.parse({_payload})
  broadcast {_gs}.toJson({_je})

Compare it to Java

import org.json.JSONObject as jsonObject


  String payload = "{"string":"A","number":1,"boolean":true,"null": null,"list": [1,true,"done"], "dict": {"done": true}}"
  jsonObject object = new jsonObject(payload)
  System.out.println(object.toString())

RESULT


[15:53:18 INFO]: {"blank":false,"bytes":[123,39,115,116,114,105,110,103,39,58,39,65,39,44,39,110,117,109,98,101,114,39,58,49,44,39,98,111,111,108,101,97,110,39,58,116,114,117,101,44,39,110,117,108,108,39,58,110,117,108,108,44,39,108,105,115,116,39,58,91,49,44,116,114,117,101,44,39,100,111,110,101,39,93,44,39,100,105,99,116,39,58,123,39,100,111,110,101,39,58,116,114,117,101,125,125],"empty":false}

[15:58:42 INFO]: Debug with GSON

[15:58:42 INFO]: {
[15:58:42 INFO]:   "string": "A",
[15:58:42 INFO]:   "number": 1,
[15:58:42 INFO]:   "boolean": true,
[15:58:42 INFO]:   "list": [
[15:58:42 INFO]:     1,
[15:58:42 INFO]:     true,
[15:58:42 INFO]:     "done"
[15:58:42 INFO]:   ],
[15:58:42 INFO]:   "dict": {
[15:58:42 INFO]:     "done": true
[15:58:42 INFO]:   }
[15:58:42 INFO]: }
> 
#

So yes im sure

craggy pecan
#

What do you mean compare it to Java? That's not Java

#

I see no rationale why you'd be sure.

inner jewel
#

It’s similar

#

It’s a Java in micrraft

gray spade
#

no, it's called "script"

inner jewel
#

Skript

#

SkriptLang

#

But you can read the Skript-reflect docs

#

It’s importing Java classes

craggy pecan
#

You may have the impression it's similar, but at the end of the day it does different things

inner jewel
#

Same issue happen inside java, but inside Java i got empty file.

package cz.coffee.gson.gson;

import com.google.gson.Gson;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@SuppressWarnings("unused")

public class gsonFileHandler {

    String directory;
    String filename;

    boolean force = false;
    boolean isExist = false;
    boolean isArray = false;


    public void init(String file, Integer type, boolean array, boolean force) {
        if ( type == 1 ) {
            createFile(file, force);
        }

    }
    private int createFile(String filename, boolean force){

        final String cleanPath = filename.replaceAll("[^/]+$", "");

        if ( force ) {
            new File(cleanPath).mkdirs();
        }

        File file = new File(filename);

        if ( file.exists()) {
            gsonError.gsonE(List.of("File exist"),1, false, true);
        } else {
            boolean created = false;
            try {
                created = file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (created) {
                try {
                    FileWriter writer = new FileWriter(file);
                    PrintWriter pw = new PrintWriter(writer);
                    Gson gson = new Gson();

                    pw.write("AA");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                gsonError.gsonE(List.of("File cannot be created"), 1, false, true);
            }
        }
        return 0;
    }
}

@here

craggy pecan
#

Is that thing supposed to do something?

#

Your opening message showed a proper use of JSONObject. Ever since you've been off-topic

#

There was just the issue of that undeclared variable