2

I call binance- api and response is like this:

[
 [
    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"
]

]

it has no json object. When I use retrofit, I get

JsonSyntaxException : Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 3 path $[0]

How can I parse it?

H.Gheisari
  • 250
  • 1
  • 15
  • You need to play around Arrays. This is absolutely possible and you can do it. you need to look into https://stackoverflow.com/questions/8371274/how-to-parse-json-array-with-gson – Pankaj Kumar Jun 06 '18 at 10:04

4 Answers4

1

Use it something like this and please try to create your child array to object otherwise you confused to get data from child array because you need to use this 0,1,2,3.... as your key.

 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());
            }
        });
}

Let me know if it works for you.

Sandeep Parish
  • 1,888
  • 2
  • 9
  • 20
1

You need to parse manually this json. Please check below solution:-

 JSONArray jsonArray = new JSONArray(response);
    for(int i=0;i<jsonArray.length();i++){
        String value = jsonArray.get(i);
    }
Anand Jain
  • 2,365
  • 7
  • 40
  • 66
1

You should to parse API response like below code:

  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);
         }
  }

Hope this help you...if you need any help you can ask

Mahesh Vayak
  • 1,056
  • 12
  • 25
0

Retrofit expects the json object to start with a { and end with a } Maybe you can add them if they're not already there?

Arnaud VdP
  • 227
  • 1
  • 9
  • Can you save the string before having it parsed? If so you can just add the tokens there. `String result = "{" + response + "}"` – Arnaud VdP Jun 06 '18 at 10:02
  • Otherwise you can check [this](https://stackoverflow.com/questions/28628513/how-to-parse-json-array-without-any-object-in-retrofit) link that also has a solution – Arnaud VdP Jun 06 '18 at 10:05