Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove quote from the JSONArray output

On the successful call, I am getting the JSONArray with the key "objects" and again the testValue with the key "name". The output is Like:

"Abcd"
"Wxyz"

My code is as follows:

public void onSuccess(JSONValue val) {
    JSONObject obj = val.isObject();
    JSONArray test = JSONUtil.getJSONArray(test, "objects");
    for (int i = 0; i < test.size(); i++) {
        JSONObject childJSONObject = (JSONObject) test.get(i);
        JSONValue testValue = childJSONObject.get("name");
        System.out.println(testValue);
    }
}

Want to print the name as following: (Without Double Quote)

Abcd
Wxyz
like image 897
Ask Avatar asked Feb 19 '14 11:02

Ask


People also ask

How do I remove a specific element from a JsonArray?

You can remove an element from the JSONArray object using the remove() method. This method accepts an integer and removes the element in that particular index.

How do you remove a quote from a string?

Use the String. replaceAll() method to remove all double quotes from a string, e.g. str. replaceAll('"', '') . The replace() method will return a new string with all double quotes removed.

How do I remove a quote from a Dataframe in R?

Sometimes column values in an R data frame have single quote associated with them and to perform the analysis we need to remove that quote. Therefore, to remove single quote from string column, we can use gsub function by defining the single quote and replacing it with blank(not space) as shown in the below examples.

How do you remove quotes in Java?

You can use String. replaceAll() with an empty replacement string.


2 Answers

1. .replaceAll()

testValue.toString().replaceAll("\"", "");

This method replace all the double quotes which are present in your name not the first and the last.

Example : "Abcd" becomes Abcd but if the name is "Ab"cd" it should be Ab"cd according to your requirement but it becomes Abcd. Mean to say that all the double quote replaced.

2. substring()

If you want to use the substring method approach then use the following syntax to remove the first and the last double quotes from your string:

testValue.toString().subString(1,testValue.toString().length()-1);

1 - indicates the first character of the string

testValue.toString().length()-1 : indicates the last character of the string.

For your case .substring() method is more better than the .replaceAll(), if .getString() not working.

3. .ValueOf() OR .getString()

Don't know In your case why it is not working ? (may be because the string itself containing the quotes) other wise the best way is to Convert the JSONValue to the String as String.ValueOf(testValue);

OR

childJSONObject.getString("name");

Otherwise give preference as : 3 > 2 > 1

like image 55
Vaibhav Jain Avatar answered Nov 13 '22 06:11

Vaibhav Jain


This thread is pretty old, but I stumbled upon the same problem an hour ago and found the correct solution. You have to check for the type of JsonValue and if its a JsonString, you can parse it to JsonString and call its getString() method.

if(val.getValueType().equals(JsonValue.ValueType.STRING)) {
  c.setValue(((JsonString) val).getString());
} else {
  c.setValue(val.toString());
}
like image 36
user2070007 Avatar answered Nov 13 '22 04:11

user2070007