Java JSONObject到字符串Android
如果没有这些括号,如何将像Java JSONObject到字符串Android,java,android,json,Java,Android,Json,如果没有这些括号,如何将像“{hello1:hi,hello2:hey}”这样的JSONObject转换为“hello1:hi,hello2:hey” 我知道有机会使用JSONObject.tostring,但接下来我会得到一个带括号的字符串 谢谢大家。只需替换子字符串或字符串即可 伪子字符串示例: JSONObject a = new JSONObject("{hello1: hi, hello2: hey}"); String b = a.toString().substring(1, a
“{hello1:hi,hello2:hey}”这样的JSONObject转换为“hello1:hi,hello2:hey”
我知道有机会使用JSONObject.tostring,但接下来我会得到一个带括号的字符串
谢谢大家。只需替换子字符串或字符串即可
伪子字符串示例:
JSONObject a = new JSONObject("{hello1: hi, hello2: hey}");
String b = a.toString().substring(1, a.toString().length() - 1);
伪字符串替换示例:
JSONObject a = new JSONObject("{hello1: hi, hello2: hey}");
String b = a.toString().replace("{", "");
String c = b.toString().replace("}", "");
假设您确实想做一些比您的问题所建议的更详细的事情,并且您可以对将要使用的JSON进行一些假设,您可以执行以下操作以获得您想要的任何输出格式
JSONObject json = new JSONObject("{hello1: hi, hello2: hey}");
StringBuilder sb = new StringBuilder();
for(String k : json.keys()) {
sb.append(k);
sb.append(": ").
sb.append(json.getString(k));
sb.append(", ")
}
// do something with sb.toString()
再说一遍,我可能读得太多了(在这种情况下,@ProgrammerXR的答案就可以了)。你需要拼写长度
而不是长度
:)