1

When user login to the app I am getting JSON response with username, pasword, token, userID and onboarding. As per the requirement of the application I hava to redirect the user to home page if this onboarding value is "true" in the JSON data.

[
    {
    "display_name": "my email ID",
    "msg_status": "success",
    "userid": "userID",
    "plan_type": "none",
    "token": "my token",
    "requires_onboarding": true
    }
]

I get all these fields in my java code. Now I want to let user go to the home screen if this onboarding value is true. Please help.

Aalap Patel
  • 2,058
  • 2
  • 17
  • 31

3 Answers3

1

If you get this json as a String, parse it like the following code. IF you are already with a JSONArray, then you won't need to use the first line.

JSONArray jsonArray = new JSONArray(jsonString);
JSONObject json2 = jsonArray.get(0);
boolean onBoarding = json2.getBoolean("requires_onboarding");

Then you can do whatever you want with your boolean variable.


If you have questions about JSON, here's a little information about it:

JSONArray is something like: events:[ {"object1": value}, {"object1":Value} ] Which means that you get the JSONArray and use jsonArray.get(position) for each thing inside it. (Note that you can have JSONArrays inside JSONArrays.

JSONObject is basically the "string" name with a value attached.

So you'll always have to see how the data you're getting is structured to parse right and choose correctly between JSONObjects or Arrays.

Here is a complete explanation the difference.

Community
  • 1
  • 1
George
  • 6,886
  • 3
  • 44
  • 56
1

Simplest way to do this:

boolean what = false;
try{
  JSONArray mRootArray = new JSONArray(jsonString);
  JSONObject jInnerObject = mRootArray.getJSONObject(0);
  what = jInnerObject.getBoolean("requires_onboarding");
 }catch(Exception e){
  what = false;
  e.printstacktrace();
 }

Now maintain as per state of boolean:

if(what){
// Go to netxt screen
}else{
// Stay here
}

Hope this will help you,

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
1

I got the required output guys thank you for your response it is as I mentioned below.

    String name = list.get(5);
    if (name.equals("true"))
    startActivity(new Intent(Login.this, OnBoardingOne.class));
Aalap Patel
  • 2,058
  • 2
  • 17
  • 31