Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取包含JSONObject的列表的大小?_Java_Json_List - Fatal编程技术网

Java 如何获取包含JSONObject的列表的大小?

Java 如何获取包含JSONObject的列表的大小?,java,json,list,Java,Json,List,我在列表中有一个jsonobject,它的类型为string,列表包含两个json对象,但大小为4 JSONObject存储在列表中的- [{"size":"S", "id":11}, {"size":"8", "id":19}] List<String> myList = new ArrayList<>(Arrays.asList(cart.split(","))); [{“大小”:“S”,“id”:11},{“大小”:“8”,“id”:19}] List my

我在列表中有一个jsonobject,它的类型为string,列表包含两个json对象,但大小为4

JSONObject
存储在列表中的-

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

List<String> myList = new ArrayList<>(Arrays.asList(cart.split(","))); 
[{“大小”:“S”,“id”:11},{“大小”:“8”,“id”:19}]
List myList=newarraylist(Arrays.asList(cart.split(“,”));

给出上述输出,但
myList.size()
给出4。

首先必须映射到对象,然后才能获得正确的对象计数,例如:

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 + '\'' +
            '}';
        }
    }
}
希望这能帮到你

Use Object Mapper library.

    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.core.type.TypeReference;
    import com.fasterxml.jackson.databind.ObjectMapper;
您的字符串对象需要转换为Java类对象,使用该列表可以正确地获得列表的大小

你的密码在这里

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());
}
stringcart=。。。;
ObjectMapper mapper=新的ObjectMapper();
List classObjects=null;
试一试{
classObjects=mapper.readValue(购物车,新类型引用(){});
}捕获(例外e){
抛出新的IndsolvException(ErrorFactory.INTERNAL_SERVER_ERROR,e.getMessage());
}

您的
字符串中总共有3个逗号,因此有4个部分。整个部分
“[{”size:“S”,“id”:11},{”size:“8”,“id”:19}]
也是一个字符串吗?使用
toString()
?您需要使用JSON解析库来处理该字符串。您可以发布更多代码吗?数据结构的确切类型是什么?您将json字符串视为普通字符串,这是错误的。首先使用json库将json解析为对象。然后可以使用json对象获取值。请参阅下面的链接,了解我在github上发布代码的示例,请访问:
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());
}