Remove quote from the JSONArray output

不问归期 提交于 2019-11-30 03:37:32

问题


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

回答1:


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




回答2:


Convert the JSONValue back to String:

String.ValueOf(testValue);

Or

childJSONObject.getString("name");

After all this and still end up with quotes - then fall back on RegExp.

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



回答3:


just

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

removes all " characters in your string.

or in your case

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



回答4:


You can get the value as a String itself, don't need to cast or replace:

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

It will return a String without quotes.

More info: JSONObject




回答5:


In case of using javax.json.JsonArray you can get the String of an JsonValue so:

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



回答6:


quotes in arrays depends on which JOSN Lib are using:

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




回答7:


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());
}



回答8:


you can use the below to get string without double quotes

    JsonObject childJSONObject = new JsonObject(); 
   JSONValue testValue = childJSONObject.get("name").getAsString();

this gives the string without double quotes " " in the string




回答9:


Best option is to use .getString() method of JSONObject or .valueOf() method of String. You can also use the .replaceAll("\"",""), but this has some problems, when the string itself contains "(double quotes) embedded in it. So, avoid using the replaceAll() method to get out of the unexpected problems.

But still, if you want to code on own by your hand or If you want to know how to delete the double quotes without using getString() and .valueOf(), you can try like this

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

it will give the sub string excluding first and last characters of the String. But, it seems like some messy thing.




回答10:


Assuming com.google.gwt.json.client.* here (from question tags and use of JSONValue which is hardly found anywhere else), toString() is meant to give you a JSON representation of the value.

Here, the value is a JSONString, so you need to use:

testValue.isString().stringValue()



回答11:


JSON.parse(JSON.Stringify(this_has_quotes));

Is all you need! JSON.Stringify() will get the value of the key accordingly. And JSON.parse() will parse your value and return the "dry" value without quote.




回答12:


What worked for me is

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


来源:https://stackoverflow.com/questions/21879568/remove-quote-from-the-jsonarray-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!