Convert one JSONAray item into multiple Items

后端 未结 2 1047
谎友^
谎友^ 2021-01-22 07:55

I have the following String which is returning from the Database in the form of List, My assumption is that, the list contains 3 items. but it is showing only \"1\" as the size.

相关标签:
2条回答
  • 2021-01-22 08:15

    You have to use GSON library to parse this JSON array. You need to do something similar to below code.

        JSONArray jsonArray = new JSONArray(jsonArrayString);
        List<String> list = new ArrayList<String>();
        for (int i=0; i<jsonArray.length(); i++) {
            list.add( jsonArray.getString(i) );
        }
    
    0 讨论(0)
  • 2021-01-22 08:19

    1.) create json array by passing your string to constructor

    2.) traverse array and retrieve your jsonObject using index i

    3.) fetch the activity jsonobject and then simply use your index to fetch the values for jsonActivityItem object.

    4.) create a POJO class to store your object in a collection like List etc but make sure to mark them private and use getter and setters for best practice

    class User{
        String id,kind,result;
        int userId,accountId;
        long timestamp;
        //.. getter & setters 
    }
    

    Note : To convert your string to json compatible you can use JsonParser , try this link JsonParser snippet

        JSONArray array;
        List<User> listUser=new ArrayList<>(); // to store objects as details
        try {
            array=new JSONArray(st); // st is your string ( make sure to use jsonparser )
            JSONObject jsonActivity;
            JSONObject jsonActivityItem;
            User user;
            for (int i=0;i<array.length();i++) {
                System.out.println();
                jsonActivity=array.getJSONObject(i);
                jsonActivityItem=jsonActivity.getJSONObject("activity");                
    

                // To store data for later use
                user = new User();
                user.setId(jsonActivityItem.getString("id"));
                user.settimestamp(jsonActivityItem.getLong("timestamp"));
                //..set other values using setters 
                listUser.add(user);
    

                // to display items
    
                String id        = jsonActivityItem.optString("id");
                String kind      = jsonActivityItem.optString("kind");
                String userId    = jsonActivityItem.optString("userId");
                String accountId = jsonActivityItem.optString("accountId");
                String timestamp = jsonActivityItem.optString("timestamp");
                String result    = jsonActivityItem.optString("result");
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    

    I would recommend an easy and short way i.e using gson to convert it into list but you will required to use POJO class you can take a look at this link to create a compatible POJO class and later just use Gson code

    Nullity check : Sometime you may see some exceptions when the values are not available so in those cases when you are not sure about the presence of value so use optInt optBoolean etc which will simply return the default value if it is not present and even try to convert value to int if it is string like

    from docs

    Get an optional int value associated with a key, or the default if there is no such key or if the value is not a number. If the value is a string, an attempt will be made to evaluate it as a number.

    int block_id = jObj.getInt("key",defaultvalue);
    

    e.g

    int block_id = jObj.getInt("userId",0); 
    
    0 讨论(0)
提交回复
热议问题