java - 使用 GSON 在 Java 中解析 JSON

标签 java arrays json gson

我需要解析一个像这样的数组:

[  
   [  
      1527773520000,
      "0.07608800",
      "0.07609800",
      "0.07603200",
      "0.07609300",
      "34.57200000",
      1527773579999,
      "2.62910731",
      73,
      "5.00000000",
      "0.38045959",
      "0"
   ],
   [  
      1527773580000,
      "0.07604100",
      "0.07610000",
      "0.07600700",
      "0.07603100",
      "46.01600000",
      1527773639999,
      "3.49973303",
      127,
      "17.22500000",
      "1.31058409",
      "0"
   ],
   [  
      1527773640000,
      "0.07609200",
      "0.07609800",
      "0.07601500",
      "0.07609700",
      "68.49900000",
      1527773699999,
      "5.21157734",
      102,
      "61.83300000",
      "4.70470068",
      "0"
   ],
   [  
      1527773700000,
      "0.07609700",
      "0.07609800",
      "0.07602200",
      "0.07609400",
      "60.61400000",
      1527773759999,
      "4.61160915",
      71,
      "46.85800000",
      "3.56536691",
      "0"
   ],
   [  
      1527773760000,
      "0.07605900",
      "0.07610000",
      "0.07604900",
      "0.07605000",
      "136.51000000",
      1527773819999,
      "10.38675618",
      163,
      "92.85000000",
      "7.06510307",
      "0"
   ]
]

我使用以下代码来获取上述数组,然后尝试解析它,如代码中所示。但它不起作用,因为我是 Java 新手。您能更正我的代码或向我学习新代码吗?我应该如何解析数组?

非常感谢。

import java.io.*;  
import java.net.*;
import java.util.*;
import com.google.gson.*;

public class myURLConnection
{
    public static void main(String[] args) 
    {
        try
        {
            URL url;
            url = new URL("https://api.binance.com/api/v1    /klines?symbol=ETHBTC&interval=1m&limit=5");
            URLConnection urlcon = url.openConnection();
            BufferedReader br = new BufferedReader(new InputStreamReader
                                            (urlcon.getInputStream()));
            String i;
            while ((i = br.readLine()) != null) 
            {
                System.out.println(i);
                Gson gson = new Gson();
                String[] pairString;
                pairString = gson.fromJson(i, String.class);
                double test1;
                test1 = pairString[1][4] + pairString[0][4];
                System.out.format("This is the summation: %d", test1);

            }
        } 

        catch (Exception e) 
        {
            System.out.println(e);
        }
    }
}

最佳答案

试试这个代码:

BufferedReader br = new BufferedReader(new InputStreamReader
                                (urlcon.getInputStream()));
StringBuilder builder = new StringBuilder();
while ((i = br.readLine()) != null) 
{
    builder.add(i);
}
Gson gson = new Gson();
String[][] pairString;
pairString = gson.fromJson(builder.toString(), pairString.getClass());

关于java - 使用 GSON 在 Java 中解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50625884/

相关文章:

javascript - 如何将包含数组的对象添加到类中

python - 重建 numpy 数组,需要更快的方法

javascript - 克隆表行在 js 中不起作用

java - 为什么在 T 是类型参数而 t 是变量的情况下不允许使用 "t instanceof T"?

java - MigLayout中push、grow、fill有什么区别?

Python:通过键获取字典中的值并用它们创建数组

java - gson 嵌套自定义序列化器

java - 如何在运行时从 HTML 代码片段(由浏览器解释)生成图像

java - 获取文件夹和子文件夹 ID Google Drive

json - 如何在cakephp中保存JSON数据