Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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中提取JSONArray_Java_Json_Poloniex - Fatal编程技术网

Java 从不包含双引号的JSONObject中提取JSONArray

Java 从不包含双引号的JSONObject中提取JSONArray,java,json,poloniex,Java,Json,Poloniex,我想从不包含双引号的JSONObject中提取JSONArray HTTP响应实体如下所示 {“asks”:[[“107.47649000”,25.3039],“bids”:[[“107.06385000”,64.9317],“isfreeze”:“0”,“seq”:298458396} 具体来说,我需要提取107.4764900和25.3039。 但是,值25.3039不包含双引号 我的密码在下面 public static void bid_ask () throws ClientP

我想从不包含双引号的JSONObject中提取JSONArray

HTTP响应实体如下所示

{“asks”:[[“107.47649000”,25.3039],“bids”:[[“107.06385000”,64.9317],“isfreeze”:“0”,“seq”:298458396}

具体来说,我需要提取107.4764900和25.3039。 但是,值25.3039不包含双引号

我的密码在下面

    public static void bid_ask () throws ClientProtocolException, IOException {
    String queryArgs = "https://poloniex.com/public?command=returnOrderBook&currencyPair=USDT_ETH&depth=1";
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost post = new HttpPost(queryArgs);
    CloseableHttpResponse response = httpClient.execute(post);
    HttpEntity responseEntity = response.getEntity();
    System.out.println("Poloniex ETHUSDT");
    //System.out.println(response.getStatusLine());


    if (responseEntity != null) {
        try (InputStream stream = responseEntity.getContent()) {
             BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
             String line;

             while ((line = reader.readLine()) != null) {
                System.out.println(line);

                JSONObject jsonObj = new JSONObject(line);
                JSONArray tokenListBid = jsonObj.getJSONArray("bids");
                JSONArray tokenListAsk = jsonObj.getJSONArray("asks");
                JSONArray bid_element;
                JSONArray ask_element;
                double bid[] = new double[2];
                double ask[] = new double[2];

               System.out.println("Poloniex Bid");
                if (tokenListBid != null) { 
                   for (int i=0;i<2;i++){
                      bid_element = tokenListBid.getJSONArray(i);
                      bid[i]=bid_element.getBigDecimal(i).doubleValue();
                      System.out.println(bid[i]);
                   } 
                }


             } //end while()
        }
    }
publicstaticvoidbid\u ask()抛出ClientProtocolException,IOException{
字符串queryArgs=”https://poloniex.com/public?command=returnOrderBook¤cyPair=USDT_ETH&depth=1";
CloseableHttpClient httpClient=HttpClients.createDefault();
HttpPost=新的HttpPost(queryArgs);
CloseableHttpResponse response=httpClient.execute(post);
HttpEntity responseEntity=response.getEntity();
System.out.println(“Poloniex ethudt”);
//System.out.println(response.getStatusLine());
if(responseEntity!=null){
try(InputStream=responseEntity.getContent()){
BufferedReader reader=新的BufferedReader(新的InputStreamReader(流));
弦线;
而((line=reader.readLine())!=null){
系统输出打印项次(行);
JSONObject jsonObj=新的JSONObject(行);
JSONArray tokenListBid=jsonObj.getJSONArray(“bids”);
JSONArray tokenListAsk=jsonObj.getJSONArray(“asks”);
JSONArray bid_元素;
JSONArray ask_元素;
双标[]=新双标[2];
双重询问[]=新的双重询问[2];
System.out.println(“Poloniex投标”);
如果(tokenListBid!=null){

对于(int i=0;i
[[[“107.06385000”,64.9317]]
是一个
1*2
数组,
tokenListBid
引用它。因此不能使用
tokenListBid.getJSONArray(1);
对其进行索引。

将内部数组视为一个对象数组,并根据需要对其进行类型转换。
下面是解析“asks”部分的简化示例

更新
下面是访问数据的另一种方法

JSONObject jsonObj = new JSONObject(line);
SONArray asks = jsonObj.getJSONArray("asks").getJSONArray(0);

Double price = Double.valueOf(asks.getString(0));
Double qty = Double.valueOf(asks.getDouble(1));

System.out.printf("Ask price: %.6f, quantity %.4f", price, qty);
这是我用来测试代码的字符串

String json = "{\"asks\":[[\"107.47649000\",25.3039]],\"bids\":[[\"107.06385000\",64.9317]],\"isFrozen\":\"0\",\"seq\":298458396}";
这就是我得到的结果

要价:107.476490,数量25.3039


不是其中一个值本身不是双引号,而是引用的一个是字符串,另一个是数值。这些值不能在同一个数组中混合。谢谢George。那么我如何提取数值呢?我不确定。binance API中的相同代码可以正确工作。这不起作用。我无法从t检索数量因为JSONArray缺少双引号,所以他选择了JSONArray。@user1349407 json响应中没有“数量”字段。请解释一下您的意思。我知道没有“数量”字段。我的意思是“数值”其中不包含双引号。如64。9317@user1349407但是我的答案确实在
else
子句中处理了这个问题。你在否决投票之前试过我的答案吗?@user1349407它怎么不起作用,我正在测试你在问题中提供的字符串,它对我起作用。如果你想要帮助,你需要更清楚你的问题是什么是
String json = "{\"asks\":[[\"107.47649000\",25.3039]],\"bids\":[[\"107.06385000\",64.9317]],\"isFrozen\":\"0\",\"seq\":298458396}";