从JSONArray输出删除报价 [英] Remove quote from the JSONArray output

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

问题描述

在成功的电话,我得到的JSONArray用钥匙对象,并再次testValue用钥匙名。输出类似:

ABCD
WXYZ

我的code是如下:

公共无效的onSuccess(JSONValue VAL){
    JSONObject的OBJ = val.isObject();
    JSONArray测试= JSONUtil.getJSONArray(测试中,物);
    的for(int i = 0; I< test.size();我++){
        JSONObject的childJSONObject =(JSONObject的)test.get(I)
        JSONValue testValue = childJSONObject.get(名称);
        的System.out.println(testValue);
    }
}

要打印的名称如下:(不含双引号)

ABCD
WXYZ


解决方案

1。 .replaceAll()

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

这个方法取代所有的双引号这是present你的名字不是第一个和最后一个。

例:ABCD成为ABCD,但如果名称是ABCD应该是AB根据您的要求CD,但它变得ABCD。意思是说,所有的双引号替换。

2。子()

如果您要使用的子方法的方法,然后使用下面的语法来删除第一,并​​从字符串的最后一个双引号:

  testValue.toString()子(1,testValue.toString()长() -  1);

1 - 表示字符串的第一个字符

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

对于您的情况 .substring()方法比 .replaceAll()越多越好,如果 .getString()不工作。

3。 .ValueOf()或.getString()

不知道你的情况,为什么它不工作? (可能是因为字符串本身包含引号)其他明智的最好办法是将JSONValue转换为字符串为将String.valueOf(testValue);

childJSONObject.getString(名称);

否则会给preference为:3> 2> 1

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. .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

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

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