How to Parse JSON Array Without Any Object in Retrofit

How to fetch JSON array without any key in Retrofit?

To Parse such an Array, you can use JSONArray as :

//For kotlin
val jsonArray = JSONArray(yourArrayString)
//For Java
JSONArray jsonArray = new JSONArray(yourArrayString);

In case you are extracting it from a JSON response which also contain other objects then you can use JSONObject with it as:

val jsonObject = JSONObject(yourJSONResponse)
val yourJSONArray: String = jsonObject.getString(" Key of Your JSONArray ")
val jsonArray = JSONArray(yourJSONArray)

In this case, we are extracting the Array string from JSON response using its key and then parsing the string as JSONArray later.

Remember that, I've used JSONArray which is org.json.JSONArray instead of JsonArray which of GSON.

How to fetch JSON array without any key in Retrofit (Android)?

You Can define a Class representing the JSON Object

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Meeting{

@SerializedName("Type")
@Expose
private String type;
@SerializedName("Name")
@Expose
private String name;
@SerializedName("StartDate")
@Expose
private String startDate;
@SerializedName("EndDate")
@Expose
private String endDate;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getStartDate() {
return startDate;
}

public void setStartDate(String startDate) {
this.startDate = startDate;
}

public String getEndDate() {
return endDate;
}

public void setEndDate(String endDate) {
this.endDate = endDate;
}

}

after that you define Callback for retrofit like that
Call<List<Meeting>> getMeetings();

How to get JSON Array without object key using retrofit

    resp.getItem().getVideo().getUrlList().forEach((url) -> {
Log.i("url", url) // you can get each url here
}
);

you are basically getting a list from server.

you don't need keys to access them. you can store them in list and also do

list.get(0) - for first url

list.get(1) - for second url

and so on.

Android - JSON parsing without the array title using Retrofit

On your service code just return a Call<ArrayList<Category2>>

@GET("/v3/projects/{projectId}/categories/")
Call<ArrayList<Category2>> getProjectCategories(@Path("projectId") String projectId, @Header("Token") String token);

The parsing will be made correctly by retrofit this way.

EDIT

How you call this service:

categoryService =
CategoryClient.getClient().create(CategoryService.class);

Call<ArrayList<Category2>> call = categoryService.getProjectCategories(projectId,token);
call.enqueue(new Callback<ArrayList<Category2>>() {
@Override
public void onResponse(Call<ArrayList<Category2>> call, Response<ArrayList<Category2>> response) {
listCategories = response.body();
System.out.println("Size: " + listCategories.size().toString());
}

@Override
public void onFailure(Call<ArrayList<Category2>> call, Throwable t) {
// Log error here since request failed
Log.e(TAG, t.toString());
}
});

How to parse JSON response Array of object, where array is without key name

Create One Class Like,

class Test {

public List<TestValue> testValues;
}

Then call API,

Call<List<Test>> getTestData(@Field("xyz") String field1);

Call <List<Test>> call = service.getTestData("val");

call.enqueue(new Callback<List<Test>>() {
@Override
public void onResponse(Call<List<Test>> call, Response<List<Test>>
response) {

List<Test> rs = response.body();

}

@Override
public void onFailure(Call<List<Test>> call, Throwable t) {

}
});

User Your Model class, this is only for example purpose.

Retrofit parsing json array without key

public class ResultDTO {

@SerializedName("FIELDS")
private com.universal.jainconnection.data.FIELDS mFIELDS;
@SerializedName("PROS")
private List<Object> mPROS;
}

public class FIELDS {

@SerializedName("ID")
private String mID;
}

replace this

Observable<List<ResultDTO >> getProductsInCatalog(@Query("section") int id_selection);

How to parse json array of objects inside object using Retrofit

Try to use next class in response object:

data class PokedexResponse (
@SerializedName("pokemon")
val pokemons: List<Post>
)

interface SimpleApi {
@GET("pokedex.json")
suspend fun getCustomPosts(): Response<PokedexResponse>
}

My guess is that you missed to parse pokemon object:

{
"pokemon": [{ ... }]
}


Related Topics



Leave a reply



Submit