簡體   English   中英

獲取不帶方括號的字符串

[英]Get string without square brackets

我有這樣的json

{"First":["Already exists"],"Second":["Already exists"]}

目前我在做

JSONObject jObject = new JSONObject(myJson);
String first = jObject.getString("First")

但是我得到這樣的結果

first = ["Already exists"]

但是我想要沒有方括號或""字符串

嘗試使用JSONArray:

    JSONArray jArray = new JSONObject(myJson).getJSONArray("First");
    String first = jArray.getString(0);

您的鍵為“ first”的json消息是Array。 因此使用應視為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();
    }

感謝@ 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));

快樂編碼

嘗試獲取Substring,例如

String first = jObject.getString("First")
String subFirst = first.substring(2,first.length()-2);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM