代码之家  ›  专栏  ›  技术社区  ›  H.Gheisari

我如何通过改型解析数组JSON?

  •  2
  • H.Gheisari  · 技术社区  · 8 年前

    我打电话 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对象。当我使用改装时,我会

    JsonSyntaxException:应为BEGIN_对象,但在处为BEGIN_数组 第1行第3列路径$[0]

    我如何解析它?

    4 回复  |  直到 8 年前
        1
  •  1
  •   Anand Jain    8 年前

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

     JSONArray jsonArray = new JSONArray(response);
        for(int i=0;i<jsonArray.length();i++){
            String value = jsonArray.get(i);
        }
    
        2
  •  1
  •   Mahesh Vayak    8 年前

    您应该像下面的代码一样解析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);
             }
      }
    

    希望这能帮助你。。。如果你需要帮助,你可以问

        3
  •  1
  •   Sandeep Parish    8 年前

    像这样使用它,请尝试创建你的子数组来对象,否则你会困惑地从子数组中获取数据,因为你需要使用这个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());
                }
            });
    }
    

    如果对你有效,请告诉我。

        4
  •  0
  •   Arnaud VdP    8 年前

    改型要求json对象以 { 以一个 } 如果它们还不存在,也许你可以添加它们?