JSONObject到String Android

tsy*_*ync 9 java android json

如何将JSONObject转换"{hello1: hi, hello2: hey}""hello1: hi, hello2: hey"没有这些括号{ }

我知道有机会使用JSONObject.tostring然后我会得到一个带括号的字符串.

谢谢大家.

Mar*_*oek 14

只需做一个子串或字符串替换.

子串示例:

JSONObject a  = new JSONObject("{hello1: hi, hello2: hey}");
String b = a.toString().substring(1, a.toString().length() - 1);
Run Code Online (Sandbox Code Playgroud)

字符串替换示例:

JSONObject a  = new JSONObject("{hello1: hi, hello2: hey}");
String b = a.toString().replace("{", "");
String c = b.toString().replace("}", "");
Run Code Online (Sandbox Code Playgroud)

  • 你需要拼写`length`而不是`lenght` :) (2认同)