提问者:小点点

如何解析数组数组JSON与改造?


我调用binance-api,响应是这样的:

[
 [
    1528265700000,
    "606.84000000",
    "609.50000000",
    "606.84000000",
    "609.50000000",
    "399.45771000",
    1528266599999,
    "242985.40248060",
    415,
    "200.23538000",
    "121838.16910200",
    "0"
 ],
 [
    1528266600000,
    "609.50000000",
    "609.58000000",
    "606.88000000",
    "608.11000000",
    "1328.29962000",
    1528267499999,
    "807439.07315600",
    709,
    "220.23076000",
    "133950.90569850",
    "0"
]

]

它没有json对象。当我使用改造时,我得到

预期BEGIN_OBJECT,但BEGIN_ARRAY在第1行第3列路径$[0]

如何解析它?


共3个答案

匿名用户

您需要手动解析这个json。请查看以下解决方案:-

 JSONArray jsonArray = new JSONArray(response);
    for(int i=0;i<jsonArray.length();i++){
        String value = jsonArray.get(i);
    }

匿名用户

你应该像下面的代码一样解析API响应:

  String result = response.body().string();
  JSONArray jsonArray = new JSONArray(result);
  for(int i=0;i<jsonArray.length();i++){
       JSONArray jsonArrayRow = jsonArray.getJSONArray(i);
       for (int j = 0; j < jsonArrayRow.length(); j++) {
                 String data=jsonArrayRow.getString(j);
         }
  }

希望这对你有帮助…如果你需要任何帮助你可以问

匿名用户

使用它类似这样的东西,请尝试创建您的子数组来对象,否则您会对从子数组获取数据感到困惑,因为您需要使用这个0,1,2,3……作为您的密钥。

 private void loadJson() {

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("you api url")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RequestInterface request = retrofit.create(RequestInterface.class);
        Call<JSONResponse> call = request.getJSON();
        call.enqueue(new Callback<JSONResponse>() {
            @Override
            public void onResponse(Call<JSONResponse> call, Response<JSONResponse> res) {

                JSONResponse response = res.body();
                try {
                    JSONArray json = new JSONArray(""+response);
                    for (int i = 0; i < json.length(); i++) {

                        JSONArray arrayInArray = json.getJSONArray(i);

                        for (int j = 0; j < arrayInArray.length(); j++) {
                            Log.e("Here is", "" + arrayInArray.getString(j));
                        }

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<JSONResponse> call, Throwable t) {
                Log.d("Error",t.getMessage());
            }
        });
}

让我知道它是否适合你。