0

I am pretty weak with JSON, and probably have a silly question, and was wondering how to parse a JSON object placed inside a JSON array. So, as of now, I have

public Single<Profile> doProfileApiCall() {
        return Rx2AndroidNetworking.post(ApiEndPoint.ENDPOINT_PROFILE)
                .addHeaders(mApiHeader.getProtectedApiHeader())
                .build()
                .getObjectSingle(Profile.class);

To retrieve my profile params, but in my endpoints I have :

[{"Name": "this", "Email","[email protected]"}]

I have my endpoint set up as :

 public static final String ENDPOINT_PROFILE =
            BuildConfig.BASE_URL
            + "/linktojson"; 

which gives me the above JSON. but the issue is the [], how do I modify this with :

 public Single<Profile> doProfileApiCall() {
            return Rx2AndroidNetworking.post(ApiEndPoint.ENDPOINT_PROFILE)
                    .addHeaders(mApiHeader.getProtectedApiHeader())
                    .build()
                    .getObjectSingle(Profile.class);

such that I can use my profile.java model class which has

public class Profile {
    @Expose
    @SerializedName("Name")
    private String name;
    @Expose
    @SerializedName("Email")
    private String email;
    etc...
}

Any idea how to go about this?

3
  • I think what you have there is a JSONArray with a single JSONObject in, i believe that this question might be of some help to you. parse JSONArray Commented Mar 7, 2019 at 20:42
  • cant i use : Rx2AndroidNetworking.post ? my array will grow soon and profile.java model class will consist of more json objects but all be within 1 array Commented Mar 7, 2019 at 20:47
  • Are you using the retrofit library for making API requests? If not can you specify the library name which you are using? Commented Mar 7, 2019 at 20:52

2 Answers 2

1

In the doProfileApiCall() method instead of .getObjectSingle use

.getJSONArraySingle(ProfileList.class)

Now create a new class ProfileList.java with the following code.

List<Profile> profileList = new ArrayList<>();

public List<Profile> getProfileList() {
    return profileList;
}

public void setProfileList(List<Profile> profileList) {
    this.profileList = profileList;
}

Change the returntype of the doProfileApiCall method to

public Single<ProfileList> doProfileApiCall()

Whenever you want to access the data use it with the list position 0, when in future you get more data, you can index the data accordingly.

Sign up to request clarification or add additional context in comments.

Comments

0

Generally, if JSON root object is an array you should use List on Java side. In your case you have array so use related method:

return Rx2AndroidNetworking.post(ApiEndPoint.ENDPOINT_PROFILE)
          .addHeaders(mApiHeader.getProtectedApiHeader())
          .build()
          .getObjectListSingle(Profile.class);

Rx2ANRequest source.

6 Comments

I get the error : Incompatible types. Required Single<Profile> but 'getObjectListSingle' was inferred to Single<List<T>>: no instance(s) of type variable(s) T exist so that List<T> conforms to Profile
@MarissaNicholas You can use map like so getObjectListSingle(Person.class) .map(list -> list.get(0)
still throws an error like "expected begin_object" but was a STRING at line 1 column 1 path 0
@MarissaNicholas, it means that JSON payload which is returned from sever is invalid. Could you show your JSON which you received?
[{"Name": "this", "Email","[email protected]"}] this is the json i receive
|

Your Answer

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