我有这样的json
{"First":["Already exists"],"Second":["Already exists"]}
目前我正在做
JSONObject jObject = new JSONObject(myJson);
String first = jObject.getString("First")
但是我得到了这样的结果
first = ["Already exists"]
但我想要没有方括号的字符串或""
答案 0 :(得分:2)
尝试使用JSONArray:
JSONArray jArray = new JSONObject(myJson).getJSONArray("First");
String first = jArray.getString(0);
答案 1 :(得分:0)
带有'first'键的json消息是Array。所以使用应该视为数组。
String string = "{'First':['Already exists'],'Second':['Already exists']}";
JSONObject jObject;
try
{
jObject = new JSONObject(string);
Object myJson = jObject.get("First");
if(myJson instanceof JSONArray)
{
JSONArray jArray = jObject.getJSONArray("First");
for (int i = 0; i < jArray.length(); i++)
{
System.out.println("val : "+jArray.getString(i));
}
}
} catch (JSONException e)
{
e.printStackTrace();
}
答案 2 :(得分:0)
感谢@ m.qadhavi,我能够弄清楚它是如何工作的
//This was my temporary solution
//get no. of garage
properties.setPropertyFeaturesGarage(jsonObject
.getJSONObject("extras")
.getString("property_garages")
.replaceAll("[\"]", "")
.replace('[',' ')
.replace(']',' '));
但是经过@ m.qadhavi解释之后,我更正了我的代码
//get land size
properties.setPropertyFeaturesLandSize(jsonObject
.getJSONObject("extras")
.getJSONArray("property_size")
.getString(0));
快乐编码
答案 3 :(得分:-1)
尝试获取像字符串一样的子串。
String first = jObject.getString("First")
String subFirst = first.substring(2,first.length()-2);