Remove quote from JSONArray output

On a successful call, I get a JSONArray with the key "objects" and again testValue with the key "name". Exit:

"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 follows: (No double quote)

Abcd
Wxyz
+4
source share
10 answers

1..replaceAll ()

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

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

Example: "Abcd" becomes Abcd, but if the name "Ab" cd "should be Ab" cd according to your requirement, but it becomes Abcd. Say that all double quotes are replaced.

2. substring ()

, :

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

1 -

testValue.toString().length()-1: .

.substring() , .replaceAll(), .getString() .

3..ValueOf() .getString()

. , ? ( , , ) JSONValue String String.ValueOf(testValue);

childJSONObject.getString("name");

: 3 > 2 > 1

+12

JSONValue String:

String.ValueOf(testValue);

childJSONObject.getString("name");

- - RegExp.

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

s.replaceAll("\"", "");

.

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

, :

JSONObject childJSONObject = (JSONObject) test.get(i);
String testValue = childJSONObject.getString("name");

.

: JSONObject

+1

, JOSN Lib:

org.codehaus.jettison.json.JSONArray org.json.JSONArray

+1

.getString() JSONObject .valueOf() String.   .replaceAll("\"",""), , " ( ), . replaceAll() .

, , , getString() and .valueOf(),

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

, . , , - .

0

javax.json.JsonArray JsonValue :

JsonArray array = jsonObject.getJsonArray("languages");
for (int i=0; i<array.size(); i++) {
     System.out.println(array.getJsonString(i).getString());
}
0

com.google.gwt.json.client.* ( JSONValue, - ), toString() JSON- .

JSONString, :

testValue.isString().stringValue()
0
JSON.parse(JSON.Stringify(this_has_quotes));

Anything you need! JSON.Stringify()will receive the corresponding key value. And it will JSON.parse()analyze your value and return a “dry” value without a quote.

-1
source

What worked for me

defect.get("FormattedID").toString().substring(1,defect.get("FormattedID").toString().length()-1)
-1
source

Source: https://habr.com/ru/post/1527699/


All Articles