0

I have jsonobject in list which is of type string and the list contains two json object but size is giving 4.

JSONObject which are stored in list are-

[{"size":"S", "id":11},  {"size":"8", "id":19}]

List<String> myList = new ArrayList<>(Arrays.asList(cart.split(","))); 

is giving above output but myList.size() is giving 4.

Tani
  • 93
  • 1
  • 12
  • 2
    There are total 3 commas in your `String`, hence there are 4 parts. Also is the entire part `"[{"size":"S", "id":11}, {"size":"8", "id":19}]"` a String? – Sid Jan 31 '19 at 07:10
  • Use `toString()`? – Raedwald Jan 31 '19 at 07:11
  • But the list contains two object how can I know the exact size? – Tani Jan 31 '19 at 07:11
  • 1
    You need to use a JSON parsing library to work with that string. – Thilo Jan 31 '19 at 07:11
  • What does your "cart" variable contain ? if it's the json string you posted, your `split(",") ` method will create an ArrayList with 4 Strings, which are the 4 tokens split with the comma delimiter – nullPointer Jan 31 '19 at 07:11
  • 1
    Can you post more code? What is the exact type of the Data Structure? – Sid Jan 31 '19 at 07:11
  • cart variable is of string stype – Tani Jan 31 '19 at 07:13
  • If you code can resolve JSONObject, then it can also resolve JSONArray. Store it in jsonarray and then fulfil your desires – uneq95 Jan 31 '19 at 07:13
  • In addition to what Thilo said, parse the strings to objcets and check field by field if it equals. ( You can implement equals() for the object ) – Itai Soudry Jan 31 '19 at 07:14
  • 1
    You treating json string like normal string which is wrong. First use a json library to parse json to a object. Then you can use json object for getting values. Refer to the below link for examples https://www.mkyong.com/java/jackson-2-convert-java-object-to-from-json/ – Santosh Balaji Selvaraj Jan 31 '19 at 07:14
  • You need to parse the json and the get the length of the array. – Maurice Perry Jan 31 '19 at 07:17
  • This will help you https://stackoverflow.com/questions/7246157/how-to-parse-a-json-string-to-an-array-using-jackson – Sudhir Ojha Jan 31 '19 at 07:33
  • Using Gson library you can mapped you object in list of object first and then count it, that will be helpful, I have added in the answer, please check it. – krishna Prasad Jan 31 '19 at 07:38
  • @Tani is your problem resolved by below ans or not? – krishna Prasad Feb 01 '19 at 13:57
  • Thank you, The problem has been resolved. I have pass string to JSONObject and getting the exact length – Tani Feb 02 '19 at 04:14

2 Answers2

1

First you have to mapped into the object then you will be able to get the correct object count e.g:

package com.ds;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;


public class Main {


    public static void main(String[] args) {

        String cart = "[{\"size\":\"S\", \"id\":11},  {\"size\":\"8\", \"id\":19}]";

        Type type = new TypeToken<ArrayList<CObject>>() {
    }.getType();

        ArrayList<CObject> cObjectList = (new Gson()).fromJson(cart, type);

        System.out.println(cObjectList);

        System.out.println(cObjectList.size());

    }

    public class CObject {

        private String id;
        private String size;

        public String getSize() {
            return size;
        }

        public void setSize(String size) {
            this.size = size;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        @Override
        public String toString() {
            return "CObject{" +
            "id='" + id + '\'' +
            ", size='" + size + '\'' +
            '}';
        }
    }
}

Output of above code is:

[CObject{id='11', size='S'}, CObject{id='19', size='8'}]
2

Hope this will help you out.

krishna Prasad
  • 3,541
  • 1
  • 34
  • 44
  • I have published my code on github, please check it at: https://github.com/krishnaiitd/learningJava/blob/master/DS/src/com/ds/Main.java#L23 – krishna Prasad Jan 31 '19 at 07:43
0
Use Object Mapper library.

    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.core.type.TypeReference;
    import com.fasterxml.jackson.databind.ObjectMapper;

Your String object needs to be converted to Java Class Object and using that list you can get the size of list correctly.

Your Code goes here.

String cart = ...;
ObjectMapper mapper = new ObjectMapper();
List<ClassName> classObjects= null;
try {
    classObjects= mapper.readValue(cart, new TypeReference<List<ClassName>>() {});
} catch (Exception e) {
    throw new IndsolvException(ErrorFactory.INTERNAL_SERVER_ERROR, e.getMessage());
}
Nagaraj S Kharvi
  • 73
  • 1
  • 2
  • 12