Reputation: 250
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?
Upvotes: 2
Views: 1150
Reputation: 2258
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.
Upvotes: 1
Reputation: 1106
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
Upvotes: 1
Reputation: 2385
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);
}
Upvotes: 1
Reputation: 267
Retrofit expects the json object to start with a {
and end with a }
Maybe you can add them if they're not already there?
Upvotes: 0