Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.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 无法从POST请求将JSON获取到数组中_Java_Json_Unirest - Fatal编程技术网

Java 无法从POST请求将JSON获取到数组中

Java 无法从POST请求将JSON获取到数组中,java,json,unirest,Java,Json,Unirest,我正在使用该库解析从MAshape API获得的一些JSON。我对Java中的HTTP请求还不熟悉,但我很难理解这个错误 我的作用是: public ArrayList<String> httpPost(int year) { HttpResponse<String> response = null; try { response = Unirest.post("https://--------.p.mashape.com/v1/calcu

我正在使用该库解析从MAshape API获得的一些JSON。我对Java中的HTTP请求还不熟悉,但我很难理解这个错误

我的作用是:

public ArrayList<String> httpPost(int year) {
    HttpResponse<String> response = null;
    try {
        response = Unirest.post("https://--------.p.mashape.com/v1/calculate/" + year)
                .header("X-Mashape-Key", "--------------------------")
                .header("Content-Type", "application/x-www-form-urlencoded")
                .header("Accept", "application/json")
                //.field("blabla", "blabla")
                .field("filing_status", myFS)
                .field("pay_periods", myPP)
                .field("pay_rate", getGrossPay())
                .field("state", myState)
                .asString();
    } catch (UnirestException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // retrieve the parsed JSONObject from the response
    JSONObject myObj = new JSONObject(response);
    JSONArray results = new JSONArray();
    try {
        results = myObj.getJSONArray("annual"));
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    ArrayList<String> list = new ArrayList<String>(); 
    if (results != null) { 
       int len = results.length();
       for (int i=0;i<len;i++){ 
        try {
            list.add(results.get(i).toString());
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       } 
    }
    return list;
}
因此,我将响应变量输出到一个文件,该文件包含com.mashape.unirest.http。HttpResponse@161b062a在里面。我不知道那是从哪里来的

只是为了检查一下,我使用了相同的方法,并且我在我的程序中输入的相同响应产生了成功的响应:

{
  "annual": {
    "state": {
      "amount": 68232.65
    },
    "fica": {
      "amount": 91800
    },
    "federal": {
      "amount": 418014.8
    }
  },
  "per_pay_period": {
    "state": {
      "amount": 5686.05
     },
     "fica": {
      "amount": 7650
    },
    "federal": {
      "amount": 34834.57
    }
  }
}

我尝试将响应类型从String更改为JsonNode,但没有成功,而且我真的不知道如何使用它。我还添加了所有必需的库。有什么帮助吗?

使用myObj.getJSONObjectannual

每年替换myObj.getjsonarrayanal;作者:myObj.Getjsonobectannal;JSONObject[annual]未找到我认为这会起作用,因为它是一个数组,但它没有。这是否意味着我得到的JSON有问题?错误中的annual上有括号,JSON是否要求数组周围有括号?JSONObject[annual]未找到我认为这会起作用,因为它是annual,但它不是数组。这是否意味着我得到的JSON有问题?错误中有括号,JSON是否需要在数组周围加括号?请尝试这种方式:JSONObject myObj=new JSONObject response.getBody;JSONObject结果=新的JSONObject;结果=myObj.getJSONObjectannual;
{
  "annual": {
    "state": {
      "amount": 68232.65
    },
    "fica": {
      "amount": 91800
    },
    "federal": {
      "amount": 418014.8
    }
  },
  "per_pay_period": {
    "state": {
      "amount": 5686.05
     },
     "fica": {
      "amount": 7650
    },
    "federal": {
      "amount": 34834.57
    }
  }
}