Java 从 JSONArray 输出中删除引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21879568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove quote from the JSONArray output
提问by Ask
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:
在成功调用时,我得到了键为“objects”的 JSONArray 和键为“name”的 testValue。输出如下:
"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
采纳答案by Vaibhav Jain
1. .replaceAll()
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.
示例:"Abcd" 变为 Abcd 但如果名称为 "Ab"cd" 根据您的要求,它应该是 Ab"cd 但它变为 Abcd。意思是说所有的双引号都替换掉了。
2. substring()
2. 子串()
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
1
- 表示字符串的第一个字符
testValue.toString().length()-1
: indicates the last character of the string.
testValue.toString().length()-1
: 表示字符串的最后一个字符。
For your case .substring()
method is more better than the .replaceAll()
, if .getString()
not working.
对于您的情况,.substring()
方法比 更好.replaceAll()
,如果.getString()
不起作用。
3. .ValueOf() OR .getString()
3. .ValueOf() 或 .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);
不知道在你的情况下为什么它不起作用?(可能是因为字符串本身包含引号)否则最好的方法是将 JSONValue 转换为字符串String.ValueOf(testValue);
OR
或者
childJSONObject.getString("name");
childJSONObject.getString("name");
Otherwise give preference as : 3 > 2 > 1
否则优先考虑:3 > 2 > 1
回答by Stefan Beike
just
只是
s.replaceAll("\"", "");
removes all " characters in your string.
删除字符串中的所有 " 字符。
or in your case
或者在你的情况下
testValue.toString().replaceAll("\"", "");
回答by Bitmap
Convert the JSONValue back to String:
将 JSONValue 转换回字符串:
String.ValueOf(testValue);
Or
或者
childJSONObject.getString("name");
After all this and still end up with quotes - then fall back on RegExp.
毕竟这一切仍然以引号结束 - 然后回到 RegExp。
testValue.toString().replaceAll("\"", "");
回答by Amin Abu-Taleb
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
更多信息:JSONObject
回答by Azeez
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.
最好的选择是使用.getString()
JSONObject 的.valueOf()
方法或 String 的方法。您也可以使用.replaceAll("\"","")
,但是当字符串本身包含"
嵌入其中的(双引号)时,这会出现一些问题。因此,避免使用 replaceAll() 方法摆脱意外问题。
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
但是,如果您想自己手动编码,或者如果您想知道如何在不使用的情况下删除双引号getString() and .valueOf()
,您可以尝试这样
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.
它将给出不包括字符串的第一个和最后一个字符的子字符串。不过,好像有点乱。
回答by Thero
In case of using javax.json.JsonArray you can get the String of an JsonValue so:
在使用 javax.json.JsonArray 的情况下,您可以获得 JsonValue 的字符串,因此:
JsonArray array = jsonObject.getJsonArray("languages");
for (int i=0; i<array.size(); i++) {
System.out.println(array.getJsonString(i).getString());
}
回答by user4605712
quotes in arrays depends on which JOSN Lib are using:
数组中的引号取决于正在使用的 JOSN Lib:
org.codehaus.jettison.json.JSONArray or org.json.JSONArray
org.codehaus.jettison.json.JSONArray 或 org.json.JSONArray
回答by Gfast2
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.
是你所需要的全部!JSON.Stringify()
将相应地获得键的值。而JSON.parse()
将分析你的价值并没有报价返回“干”的价值。
回答by Saurabh Juneja
What worked for me is
对我有用的是
defect.get("FormattedID").toString().substring(1,defect.get("FormattedID").toString().length()-1)
回答by Thomas Broyer
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.
假设com.google.gwt.json.client.*
在这里(来自问题标签,并且JSONValue
在其他任何地方都很难找到它的使用),toString()
旨在为您提供值的 JSON 表示。
Here, the value is a JSONString
, so you need to use:
在这里,该值为 a JSONString
,因此您需要使用:
testValue.isString().stringValue()