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?

1

4 Answers 4

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);
    }
Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

Why are you can't get response.bod()?
because with retrofit It is not possible to get response body.
Who told you I am already using retrofit 2.0 I can get it why are you not get which version have you use?
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.

5 Comments

of course it works. I want to do it via retrofit and Gson.
@H.Gheisari I updated my answer try it and let me know if works
what is JSONResponse?
It is response you getting in json fromet by hitting url
and after it i converted it in string
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?

2 Comments

Can you save the string before having it parsed? If so you can just add the tokens there. String result = "{" + response + "}"
Otherwise you can check this link that also has a solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.