#new JSONObject(String); doesnt work

1 messages · Page 1 of 1 (latest)

elder spearBOT
#

<@&987246964494204979> please have a look, thanks.

elder spearBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

brittle finch
#

put ur sting in map?

#

Map.of("key", "value);

minor osprey
#

@viral horizon what are you trying to accomplish exactly?

#

and what json library are you using, etc

weary dune
#

Not sure which json library will take such parameter.
But a string is not a valid json form. It either be a string array or a string value with a key in an object.

minor osprey
#

older json specs stipulated that

#

but json can be a string

#

A JSON text is a serialized value. Note that certain previous
specifications of JSON constrained a JSON text to be an object or an
array. Implementations that generate only objects or arrays where a
JSON text is called for will be interoperable in the sense that all
implementations will accept these as conforming JSON texts.

#

JSON-text = ws value ws

value = false / null / true / object / array / number / string

weary dune
#

wow did not know that

minor osprey
#

@viral horizon if you want to use it, this is how you turn a string into Json

#
import dev.mccue.json.Json;

public class Main {
    public static void main(String[] args) {
        Json value = Json.of("Hello");

        System.out.println(value);
    }
}
#

and this is how you parse a string containing json

#
import dev.mccue.json.Json;

public class Main {
    public static void main(String[] args) {
        Json value = Json.readString("""
                {
                    "message": "hello"
                }
                """);

        System.out.println(value);
    }
}
elder spearBOT
#

Detected code, here are some useful tools:

Formatted code
Exception in thread"main"java.lang.UnsupportedClassVersionError : dev / mccue / json / Json has been compiled by a more recent version of the Java Runtime(class file version61.0), this version of the Java Runtime only recognizesclass file versions up to60.0at java.base / java.lang.ClassLoader.defineClass1(Native Method) at java.base / java.lang.ClassLoader.defineClass(ClassLoader.java : 1010) at java.base / java.security.SecureClassLoader.defineClass(SecureClassLoader.java : 150) at java.base / jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java : 855) at java.base / jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java : 753) at java.base / jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java : 676) at java.base / jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java : 634) at java.base / jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java : 182) at java.base / java.lang.ClassLoader.loadClass(ClassLoader.java : 519) at Main.main(Main.java : 30)
minor osprey
#

@viral horizon I feel like there is more to what you are trying to do than just turning a string into json

#

Can you share more of your task/code you have so far?

minor osprey
#

If you give me a url i can show you how to do it without jackson

#

but if that did the job for you thats all well

#

also - try using HttpClient instead of URLConnection

minor osprey
#

everything in your screenshot

#

no clue, but making the request uses up an order of magnitude more

#

so don't worry about it

minor osprey
#

the thing you actually want to change is to close the urlConnection when you are done with it

#

but if you use http client that will work itself out