从 JSONArray 输出中删除引号 [英] Remove quote from the JSONArray output

查看:31
本文介绍了从 JSONArray 输出中删除引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在成功调用时,我得到了键为objects"的 JSONArray 和键为name"的 testValue.输出如下:

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"

我的代码如下:

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..replaceAll()

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

此方法替换您姓名中出现的所有双引号,而不是第一个和最后一个.

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

例如:"Abcd" 变成 Abcd 但如果名称是 "Ab"cd" 根据您的要求它应该是 Ab"cd 但它变成了 Abcd.意思是说所有的双引号都替换了.

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 - 表示字符串的第一个字符

1 - indicates the first character of the string

testValue.toString().length()-1 :表示字符串的最后一个字符.

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

对于你的情况 .substring() 方法比 .replaceAll() 更好,如果 .getString()不工作.

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

3..ValueOf() 或 .getString()

不知道在你的情况下为什么它不起作用?(可能是因为字符串本身包含引号)否则最好的方法是将 JSONValue 转换为字符串为 String.ValueOf(testValue);

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

childJSONObject.getString("name");

否则优先选择:3 > 2 > 1

Otherwise give preference as : 3 > 2 > 1

这篇关于从 JSONArray 输出中删除引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆