简体   繁体   中英

Remove all the Quotes from JSONArray

JSONArray error = data.getJSONArray("error");

                for (int it=0; it<error.length(); it++){
             error.toString().replaceAll("\"", " ");
                    System.out.println(error);
                }           

I got a JSON response from a SOLR link which has been parsed into a JSONArray. Here in the code am trying to remove the double quotes from the JSONArray. But its not happening. Can anyone help me at the earliest? Thanks in advance.

I see whats wrong. You are not printing the result from replaceAll call. To remove all quotes from json array output, try this.

JSONArray error = data.getJSONArray("error");
System.out.println(error.toString().replaceAll("\"", " "));

Please note, this will also remove any quotes inside array values, which might not be what you want. For example, the output of ["cool says \\"meow\\"","stuff"] would be [ cool says \\ meow\\ , stuff ] . If you only want the string values, I recommend looking at theorg.json.JSONArray docs for JSONArray::get(int) and JSONArray::length()

It looks like you are trying to print an array of json strings. If you replace all quotes it will also replace the quotes inside of strings and distort the value that was encoded. If someone did that to me, I wouldn't be very happy :). For instance look at this json.

[
  "hello",
  "cat says \"meow\"",
  "dog says \"bark\"",
  "the temperature is 15\u00B0"
]

Not only would the quotes be missing, but also the special unicode character for degrees would probably not look correct ( 15° ). To return the value in it's original form, you would need to implement the entire json spec ! Thats a lot of work, probably not something you want to do. I've done it before and it wasn't easy.

Fortunately though, we are already using a library that does all of this for us :) Just use org.json package. It has everything you need to properly encode and decode values. You didn't think you would have to do all of that parsing yourself did you? To print the strings in their original form, you could do this.

/** use these imports
 *
 * import org.json.JSONArray;
 * import org.json.JSONObject;
 * import org.json.JSONException;
 **/
JSONArray ja = new JSONArray();

// add some strings to array
ja.put("hello");
ja.put("cat says \"meow\"");
ja.put("the temperature is 15\u00B0");

// add an int
ja.put(1);

// add an object
JSONObject jo = new JSONObject();
jo.put("cool", "cool");
ja.put(jo);

// loop and print only strings
for (int i = 0; i < ja.length(); ++i) {
    try {
        // ignore null values
        if (!ja.isNull(i)) {
            System.out.println(ja.getString(i));
        }
    } catch (JSONException e) {
        // not a string
        // try ja.getBoolean
        // try ja.getDouble
        // try ja.getInt
        // try ja.getJSONArray
        // try ja.getJSONObject
        // try ja.getLong
    }
}

To use with your original code, just replace ja with your own variable. Notice in the catch clause I put some comments show other methods you could use to read values in a parsed json object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM