Java convert a Json string to an array

后端 未结 3 1332
再見小時候
再見小時候 2021-01-13 07:38

I have a Json String and I am trying to convert it to an array in Java.

public void DisplaySubjects(String subjects)
     {
         JSONObject jsonResponse;         


        
相关标签:
3条回答
  • 2021-01-13 08:08

    Something like this:

    ArrayList<String> jsonStringToArray(String jsonString) throws JSONException {
    
        ArrayList<String> stringArray = new ArrayList<String>();
    
        JSONArray jsonArray = new JSONArray(jsonString);
    
        for (int i = 0; i < jsonArray.length(); i++) {
            stringArray.add(jsonArray.getString(i));
        }
    
        return stringArray;
    }
    
    0 讨论(0)
  • 2021-01-13 08:21

    You could try something like:

    new JSONArray(jsonString)
    

    or if it is a property:

    jsonObject.getJSONArray(propertyName)
    
    0 讨论(0)
  • 2021-01-13 08:23

    You need to make JSON object first.

    For example,

    JSONObject jsonObject = new JSONObject(resp);
    

    jsonObject may contains other JSON Objects or JSON array.

    How to convert the JSON depends on the String.

    There is a complete example with some explanation about JSON String to JSON array can be found at http://www.hemelix.com/JSONHandling.

    0 讨论(0)
提交回复
热议问题