JSON conversion

Discussion in 'Plugin Development' started by The_Spaceman, Aug 17, 2019.

Thread Status:
Not open for further replies.
  1. Offline

    The_Spaceman

    I'm working on a JSON text converter, and I've a little problem

    I want to replace some " into \", but not the '' from the value.

    example {"text":"test"}, this must be converted to: {\"text\":\"test\"}.

    The problem is that if the value contains a ", this will also be converted (using .replace("\"","\\\"")
    {"text":"test " test"} -> {\"text\":\"test \" test\"}.

    What I want is that only the " from the JSON part are converted.

    Thanks for your help
     
  2. Offline

    Reflxction

    I don't think it's ideal if you write the JSON text yourself. If I recall correctly, gson is bundled inside Bukkit. You could do something like:
    Code:
            JsonObject text = new JsonObject();
            text.addProperty("text", "test");
            // ...
            String jsonText = text.toString(); // Results in {"text":"test"}
    
     
    SloopyGames likes this.
  3. Offline

    The_Spaceman

    What I have works just fine, but the only problem is that I need {\"text\":\"test\"}, with the back slash...

    If there is not a simple solution I have to add the \ while I create the JSON.
     
  4. Offline

    Kars

    @The_Spaceman you are what us developers call "reinventing the wheel". This is not good practice. All JSON converters contain string escape logic. Just use one.
     
Thread Status:
Not open for further replies.

Share This Page