0

I have tried everything online to try to parse this JSON but nothing seems to work. Here is the JSON:

{"salonphoebe":true,"salonvo":false}

That's it. It is only booleans. It is from an HTTP website if that is important at all. How do I do parse this extremely simple JSON from http://example.com in Java in Android Studio? I am trying to create Booleans based on these in my app. I know this question is on this website a lot but I have literally tried 10 solutions but nothing will work. Thank you.

2
  • 3
    Can u post the code Commented Jul 19, 2018 at 18:44
  • I have no code. I don't know where to start. All I have is a basic MainActivity.java Commented Jul 19, 2018 at 19:24

2 Answers 2

1

Try the following code.

try {
  JSONObject json = new JSONObject(your - json - string - here);
  boolean b1 = json.optBoolean("salonphoebe");
  boolean b2 = json.optBoolean("salonvo");
 } catch (JSONException e) {
  e.printStackTrace();
 }
Sign up to request clarification or add additional context in comments.

4 Comments

How can I get the JSON String from the Internet?
That code works great for parsing the JSON when I just paste the JSON into the "your-json-string-here"
So the only question left is how do I get the JSON string from online?
@emmetMiller look up Retrofit, that's the standard networking Library for Android. You also don't need the code above then, you can have GSON or any other deserializing library take care of that for you. There are tons of tutorials.
0

Okay I have solved my own problem. Here is everything I learned and what I did. I want to help anyone else with this problem if they come across this issue. First I added this to my androidmanifest.xml:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Then I added this between the tags in the androidmanifest.xml beucase the link I am parsing the JSON from is an HTTP link:

android:usesCleartextTraffic="true"

Really quickly import all of this into your mainactiviy.java:

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

Then we get into the hard stuff. There are two parts to parsing JSON data from the internet. First, you must read the JSON (meaning put the JSON from online into a String) from the URL and then you must organize the String of JSON into separate variables. So let's start on the HTTP Request. I created an Async class in my MainActivity.java (under the OnCreate) that I found online that looks like this:

public class HttpGetRequest extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        try {
            String url = "http://example.com/example.php";
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            int responseCode = con.getResponseCode();
            System.out.println("\nSending 'GET' request to URL : " + url);
            System.out.println("Response Code : " + responseCode);
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));
            String inputLine;
            System.out.println("Test");
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }

            in.close();

            String jsonResponse = response.toString();
            return  jsonResponse;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        try {
            JSONObject json = new JSONObject(result);
            boolean myJsonBool = json.optBoolean("samplestringinyourjson");
            if(hasPaid){
                //do something 
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
}

Okay so basically the reason we put this in an Async class is because java won't let you make an HTTP Request in your OnCreate. What the doInBackground is doing is fetching the JSON data and putting it into the string like I said. The OnPostExecute is separating that string into boolean values and doing stuff with it. Lastly, paste this into your OnCreate or it won't work:

new HttpGetRequest().execute();

That's it. If you have questions ask and hopefully I can see it.

1 Comment

Hi Emmett: I'm glad you found the solution on your own. The next step to try is using libraries like Volley and Retrofit for downloading content. It's far more easier than using the fundamentals :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.