AndroidDev21921
AndroidDev21921

Reputation: 695

Get value from returned json

So I'm new to json parsing in general. With the help of the community I was able to get the json returned. However, for the life of me can not figure out how to access/get certain values. I am trying to get/access the "halfjpeg" value in the "content" section.

[
  {
    "publicId": "1337",
    "name": "mypainting",
    "orientation": "vertical",
    "provider": "user",
    "providerFullName": "unknown",
    "location": {
      "coordinates": [
        -71,
        42
      ],
      "userCityCode": "BOS",
      "userCityName": "Boston",
      "userStateCode": "MA",
      "userStateName": "Massachusetts",
      "userCountryCode": "US",
      "userCountryName": "United States",
      "userZipCode": "02108",
      "latitude": 42
      "longitude": -71
    },
    "status": {
      "isLive": false,
      "isCertified": false,
      "hasVirusWarning": true
    },
    "content": {
      "halfJpeg": "userhalfpaintlocation.com",
      "fullJpeg": "userfullpaintlocation.com",
      "hugeJpeg": "userhugepaintlocation.com"
    },
    "createdAt": xxxxxxxx,
    "updatedAt": xxxxxxxx,
    "policy": {
      "isAdmin": false,
      "isUser": true
    }
  }
]

My code to get JSON is below:

URL url = null;
        try {
            url = new URL("getthepaintingurl.com");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) url.openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            conn.setRequestMethod("GET");
        } catch (ProtocolException e) {
            e.printStackTrace();
        }

        try {
            System.out.println("Response Code: " + conn.getResponseCode());
        } catch (IOException e) {
            e.printStackTrace();
        }
        InputStream in = null;
        try {
            in = new BufferedInputStream(conn.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder out = new StringBuilder();
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                out.append(line + "\n");

            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(out.toString());

        try {
            JSONObject jObject = new JSONObject(out.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }

Upvotes: 0

Views: 39

Answers (1)

Lucas Crawford
Lucas Crawford

Reputation: 3118

http://www.w3schools.com/json/json_syntax.asp

Just so you are aware of some syntax, read the above (it should help clarify!).

You the response you are expecting is in the form of a JSONArray. So it would look something like this:

//create json array from the buffer string, and get the inner json object
JSONObject response = new JSONArray(out.toString()).getJSONObject(0);

//get the json object at key "content"
JSONObject content = response.getJSONObject("content");

//access the value of "halfJpg"
String halfJpgSrc = content.getString("halfJpeg");

Comments should explain themselves, but you essentially create a JSONArray from the output from your request, and access the inner json object which contains all the information (index 0). Then you just navigate to the data you sent. In this case, the inner data is stored in key/value pairs so it should be pretty self explanatory how to access anything else within your JSON response.

Here is more documentation on those two objects (JSONArray / JSONObject)

JSONObject JSONArray

Upvotes: 1

Related Questions