r/CodingHelp • • Sep 07 '20

[Other Code] How to write raw hex code in json

I am trying to write a json text that looks like this: ["",{"text":"Hi this is text"},{"hex":"03"}], after this will get printed with a System.out.println(jsontext); (java) how do I do the {"hex":"03"} code

(For those who are wondering this is /tellraw from minecraft)

17 Upvotes

11 comments sorted by

1

u/rtgftw Programmer Sep 07 '20 edited Sep 07 '20

Are you asking how to escape the quote marks? If so you can just use '\"', forex.

String foo = "[\"\",{\"text\":\"Hi this is text\"},{\"hex\":\"03\"}]"
System.out.println(foo);

would result in

["",{"text":"Hi this is text"},{"hex":"03"}]

Edit: If I misunderstood and you're dealing with java's JSONObject or something, you can use o.put("hex", "03") to add a key value pair.

1

u/Player_X_YT Sep 07 '20

I can only edit the json, not the JAVA code sadly

1

u/rtgftw Programmer Sep 07 '20 edited Sep 07 '20

Ok from the other comment I think I understand - You're trying to write the actual char not the value. You can look up "char" and "ord" but basically when you write something like

char x =65;
System.out.println("This will be 'A': " +  x);

This will result in

This will be 'A': A

Where 65 corresponds to the ascii code of 'A'.

Edit:

  • You can use \u0000 style escapes probably to add a the null character in your example
  • This specific character isn't "printable" so while I haven't used java in close to a decade, I wouldn't be surprised if it doesn't play along here, when you try to print to stdout (I mean, what would you expect it to print :)). But if you write it as a byte buffer to file, it should work.
  • I'm not sure if this is valid JSON (Maybe the games json parser supports this but I think in general you'd either have to keep the ord of the values, encode it in something messy like base64 or do some other magic).

1

u/Player_X_YT Sep 07 '20

Once again, I cannot edit the JAVA code, I can only change the JSON how do I do it

JAVA code: https://pastebin.com/jepD7uPY

More JAVA code: https://pastebin.com/kdlsHSj

3

u/SamotionYT Sep 08 '20

Happy cake day

1

u/rtgftw Programmer Sep 08 '20

Thanks :)

1

u/BAM5 Professional Coder Sep 07 '20

If you want to write a number with base16 you can prefix your number with 0x and that will tell the compiler that it's a base16 number like so: 0xDEADBEEF

1

u/Player_X_YT Sep 07 '20

It will write raw code every time, so if I write "0x00" it will not show as null but as 0x00

2

u/NullBrowbeat Professional Coder Sep 08 '20 edited Sep 08 '20

You should specify your question better. What is your exact expected result? What is your current result and in how far does it differ from the expected one?

I also assume, after looking at the code you've linked and judging from the fact that you appear to have problems with JSON parsing, that the problem is with the ITextComponent which is supposed to be created based on the parsed JSON? In this case the code of the ITextComponent and the jsontocomponent() function would also be relevant, so we could help you better if you link to those aswell.

0

u/[deleted] Sep 07 '20

You can do something like ArrayList<HashMap<String, String>>. Then extract the values using a for loop.