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","that@gmail.com"}]

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?

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
  • 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](https://stackoverflow.com/questions/17136769/how-to-parse-jsonarray-in-android) – Nils Kähler Mar 07 '19 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 –  Mar 07 '19 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? – Atchyut Maddukuri Mar 07 '19 at 20:52

2 Answers2

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.

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.

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
  • I get the error : Incompatible types. Required Single but 'getObjectListSingle' was inferred to Single>: no instance(s) of type variable(s) T exist so that List conforms to Profile –  Mar 08 '19 at 01:31
  • @MarissaNicholas You can use map like so `getObjectListSingle(Person.class) .map(list -> list.get(0)` – Mauro Curbelo Mar 08 '19 at 02:37
  • still throws an error like "expected begin_object" but was a STRING at line 1 column 1 path 0 –  Mar 08 '19 at 03:52
  • @MarissaNicholas, it means that `JSON` payload which is returned from sever is invalid. Could you show your `JSON` which you received? – Michał Ziober Mar 08 '19 at 07:15
  • [{"Name": "this", "Email","that@gmail.com"}] this is the json i receive –  Mar 08 '19 at 19:25
  • @MarissaNicholas, but exception clearly says `but was STRING`. There must be other white character or `"`. Your `JSON` payload should be `[{"Name": "this", "Email":"that@gmail.com"}]` - semicolon not comma between `"Email"` and `"that..."`. Have you posted with mistake or it is real payload? – Michał Ziober Mar 08 '19 at 21:55