-1

AFTER CONVERTING I AMD GETTING THESE BACK BLASH WANT TO REMOVE IT ["{\"INFO_ID\":\"1\"}"]

this is how i converting

JSONArray InfoIdJsonArray1 = new JSONArray(arraylist); thanks in advance

umer saleem
  • 157
  • 1
  • 1
  • 3

3 Answers3

0

You can do like this (example):

ArrayList<String> arrayList= new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
JSONArray jsonArray = new JSONArray(arrayList);
Faz
  • 322
  • 2
  • 14
0

you can do this with Gson library easily

Gradle:

implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

Usage

    ArrayList<String> myList = new ArrayList<>();
    String jsonArrayString = (new Gson()).toJson(myList);
    JSONArray jsonArray = new JSONArray(jsonArrayString);
Ahmed Abdalla
  • 2,356
  • 2
  • 14
  • 27
0

This's a helpful solution to converting (from/to) json. And for disable escaping HTML characters :

Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();

or made a custom escaping characters as below :

val gson =
        GsonBuilder().registerTypeAdapter(String::class.java, CustomeEscapeSerializer()).create()

class CustomeEscapeSerializer : JsonSerializer<String?> {
    override fun serialize(
        s: String?,
        type: Type?,
        jsonSerializationContext: JsonSerializationContext?
    ): JsonElement {
        return JsonPrimitive(s?.let { escapeJS(it) })
    }

    companion object {
        fun escapeJS(string: String): String {
            var s = ""
            val escapes =
                arrayOf(
                    arrayOf("\\", "\\\\"),
                    arrayOf("\"", "\\\""),
                    arrayOf("\n", "\\n"),
                    arrayOf("\r", "\\r"),
                    arrayOf("\b", "\\b"),
                    arrayOf("\\f", "\\f"),
                    arrayOf("\t", "\\t")
                )
            for (esc in escapes) {
                s = string.replace(esc[0], esc[1])
            }
            return s
        }
    }
}
Ibrahim Muhamad
  • 331
  • 2
  • 8