从java中的列表json获取值

从java中的列表json获取值,java,json,Java,Json,我有一个JSonObject的列表 List<JSONObject> records = new ArrayList<JSONObject>(); 我想恢复引脚的值([43.56,1.39,43.57,1.4])。 我在我的列表上做了一个循环: for(int i = 0; i < records.size(); i++){ double pin = Double.parseDouble((Double) records.get("Code").get("

我有一个JSonObject的列表

List<JSONObject> records = new ArrayList<JSONObject>();
我想恢复
引脚的值([43.56,1.39,43.57,1.4])。
我在我的列表上做了一个循环:

for(int i = 0; i < records.size(); i++){
    double pin = Double.parseDouble((Double) records.get("Code").get("PIN"));
}
有人能帮我恢复
PIN([43.56,1.39,43.57,1.4])的值吗

多谢各位

  • 记录的类型为
    List
    ,它有一个方法
    get(int)
    ,但没有方法
    get(String)
  • Double.parseDouble
    采用
    String
    参数,而不是
    Double
  • 尝试:

    publicstaticvoidmain(字符串…参数){
    字符串JSON_String=“{”
    +“\“ID\”:15\n”
    +“\”代码\“:{\n”
    +“\'43560139\':{\'Name\':[\'AA,BB\'],\n\'PIN\':[43.56,1.39,43.57,1.4],\n\'login\':[\'1C4\']\n},\n”
    +“\'49876554\':{\'Name\':[\'CC,DD\'],\n\'PIN\':[12.34,5.67,89.01,1.4],\n\'login\':[\'1C4\']\n}\n”
    +“}\n”
    +“}\n”;
    列表记录=新的ArrayList();
    add(新的JSONObject(JSON_字符串));
    //对于记录中的每个JSONObject。。。
    for(JSONObject jsonObj:records){
    JSONObject codeObj=jsonObj.getJSONObject(“Code”);//查找“Code”
    //对于“代码”中的每个“ID”/条目:
    for(字符串id:codeObj.keySet()){
    //例如“43560139”:
    System.out.printf(“-id=%s%n”,id);
    //…获取关联的值“{”Name |:…}作为对象:
    JSONObject idData=codeObj.getJSONObject(id);
    //获取“PIN”作为数组:
    JSONArray pinArray=idData.getJSONArray(“PIN”);
    System.out.printf(““%s”的-pinArray=%s%n”,id,pinArray.toString());
    //对数组执行以下操作:
    System.out.printf(“>值:”);
    pinArray.forEach(pinValue->{
    System.out.printf(“%.2f”,Double.parseDouble(pinValue.toString());
    });
    System.out.println();
    }
    }
    }
    
    输出:

    - id=49876554 - pinArray for '49876554'=[12.34,5.67,89.01,1.4] > values: 12,34 5,67 89,01 1,40 - id=43560139 - pinArray for '43560139'=[43.56,1.39,43.57,1.4] > values: 43,56 1,39 43,57 1,40 -id=49876554 -'49876554'的pinArray=[12.34,5.67,89.01,1.4] >数值:12,34 5,67 89,01 1,40 -id=43560139 -'43560139'的pinArray=[43.56,1.39,43.57,1.4] >数值:43,56 1,39 43,57 1,40 pom.xml:

        <dependencies>
            ... 
            <!-- JSON-java: https://mvnrepository.com/artifact/org.json/json -->
            <dependency>
                <groupId>org.json</groupId>
                <artifactId>json</artifactId>
                <version>20180813</version>
                <scope>compile</scope>
            </dependency>
        </dependencies>
    
    
    ... 
    org.json
    json
    20180813
    编译
    
    应该是
    [“AA”,“BB”]
    吗?这是一个输入错误。谢谢你的回答,但我找不到函数getJSONObject,我不认识它!你在使用什么库和构建系统?我使用了与maven一起使用的函数(请参见编辑的答案)
    public static void main(String... args) {
        String JSON_STRING = "{"
                + " \"ID\": 15,\n"
                + "  \"Code\": {\n"
                + "    \"43560139\": {  \"Name\": [\"AA, BB\"],\n\"PIN\": [43.56, 1.39, 43.57, 1.4],\n\"login\": [\"1C4\"]\n },\n"
                + "    \"49876554\": {  \"Name\": [\"CC, DD\"],\n\"PIN\": [12.34, 5.67, 89.01, 1.4],\n\"login\": [\"1C4\"]\n }\n"
                + "    }\n"
                + "}\n";
    
        List<JSONObject> records = new ArrayList<JSONObject>();
        records.add(new JSONObject(JSON_STRING));
    
        // for each JSONObject in records...
        for (JSONObject jsonObj : records) {
            JSONObject codeObj = jsonObj.getJSONObject("Code"); // find 'Code'
    
            // for each 'ID?'/entry in 'Code':
            for (String id : codeObj.keySet()) {
                // eg '43560139':
                System.out.printf("- id=%s%n", id);
    
                // ... get associated value '{ "Name| :  ... } as object:
                JSONObject idData = codeObj.getJSONObject(id);
    
                // get 'PIN' as array:
                JSONArray pinArray = idData.getJSONArray("PIN");
                System.out.printf("  - pinArray for '%s'=%s%n", id, pinArray.toString());
    
                // do something with array:
                System.out.printf("      > values: ");
                pinArray.forEach(pinValue -> {
                    System.out.printf("%.2f ", Double.parseDouble(pinValue.toString()));
                });
                System.out.println();
            }
        }
    }
    
    - id=49876554 - pinArray for '49876554'=[12.34,5.67,89.01,1.4] > values: 12,34 5,67 89,01 1,40 - id=43560139 - pinArray for '43560139'=[43.56,1.39,43.57,1.4] > values: 43,56 1,39 43,57 1,40
        <dependencies>
            ... 
            <!-- JSON-java: https://mvnrepository.com/artifact/org.json/json -->
            <dependency>
                <groupId>org.json</groupId>
                <artifactId>json</artifactId>
                <version>20180813</version>
                <scope>compile</scope>
            </dependency>
        </dependencies>