Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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提取JSON中键的值_Java_Json_Jsonresponse - Fatal编程技术网

当键处于层次结构下时,使用Java提取JSON中键的值

当键处于层次结构下时,使用Java提取JSON中键的值,java,json,jsonresponse,Java,Json,Jsonresponse,我需要从下面的JSON中提取RecordOne的值 { "errors": [], "data": { "paging": { "RecordOne": 8, "RecordTwo": 9, "recordThree": 2, "totalNumberOfRecords": 86052 }, "products": [ { "testabstract": "test data",

我需要从下面的JSON中提取RecordOne的值

{
  "errors": [],
  "data": {
    "paging": {
      "RecordOne": 8,
      "RecordTwo": 9,
      "recordThree": 2,
      "totalNumberOfRecords": 86052
    },
    "products": [
      {
        "testabstract": "test data",
        "authors": "Frank Jr.",
        "invertedauthors": "Frank VJr.",
        "formatCode": "KND"
      }
     ]
   }
}
我使用Java作为语言,并使用JSON对象来实现相同的功能,下面是我使用的:

protected String getTokenValueUnderHeirarchy(String responseString){
        JSONObject json = new JSONObject(responseString);
        String val= json.getJSONObject("data").getJSONObject("paging").getString("RecordOne");
        System.out.println("val::"+val);
return val;
    }
我得到的值是val=1,应该是8

若我试图用相同的代码寻找键totalNumberOfRecords的值,它将返回正确的值,即86052


我知道这有点傻,但我听不懂。

当我用JSON示例运行您的代码时,我得到了一个“JSONException:JsonObject[“RecordOne”]不是字符串”。。。。。但事实并非如此。用双引号将8括起来:“8”返回了您期望的值。如果愿意,可以使用其他get方法访问此值:getInt

这个测试用例同时解析字符串和int。它适合你吗

package org.nadnavillus.test;

import org.json.JSONObject;
import org.junit.Test;

public class TestCase {

    protected String getTokenValueUnderHeirarchy(String responseString) throws Exception {
        JSONObject json = new JSONObject(responseString);
       String val= json.getJSONObject("data").getJSONObject("paging").getString("RecordOne");
        System.out.println("val::"+val);
        return val;
    }

    protected String getTokenValueUnderHeirarchyInt(String responseString) throws Exception {
        JSONObject json = new JSONObject(responseString);
        int val= json.getJSONObject("data").getJSONObject("paging").getInt("RecordTwo");
        System.out.println("val::"+val);
        return String.valueOf(val);
    }

    @Test
    public void testJson() throws Exception {
        String input = "{\"errors\": [], \"data\": {\"paging\": {\"RecordOne\": \"8\", \"RecordTwo\": 9, \"recordThree\": 2, \"totalNumberOfRecords\": 86052}}}";
        String test = this.getTokenValueUnderHeirarchy(input);
        System.out.println(test);
        test = this.getTokenValueUnderHeirarchyInt(input);
        System.out.println(test);
    }
}

getInt也没有帮助,我给出的JSON示例是我在Postman中使用getInt发出GET请求后得到的响应,它也给了我1作为输出。我添加了一个快速(不是最好的)JUnit测试,显示此测试同时使用getString和getInt运行。我还注意到您原来的JSON不太正确。由于缺少尾随“}”,验证失败。这可能是对示例的错误编辑。是的,实际上完整的集合工作正常,因为服务URL中存在一些差异,所以我得到的响应中的值不正确。这确实是一个愚蠢的错误。谢谢:)