DDukesterman
DDukesterman

Reputation: 1399

JSON Parsing in Android for pulling Boolean/Integer values

I am new to JSON and I was wondering how I would pull some info and put it into a JSON Object and then read some of the values. The json info looks like this,

{
    "photos":{
        "page":1, 
        "pages":1802,
         "perpage":100,
         "total":"180134", 
         "photo": [
            {
                "id":"8711964781", 
                "owner":"21156896@N07", 
                "secret":"3e24e45977", 
                "server":"8553", 
                "farm":9, 
                "title":"Old Harbor", 
                "ispublic":1, 
                "isfriend":0, 
                "isfamily":0
            }, 
            {
                "id":"8712680696", 
                "owner":"21156896@N07", 
                "secret":"fe82f8387b", 
                "server":"8122", 
                "farm":9, 
                "title":"Rockefeller Plaza", 
                "ispublic":1, 
                "isfriend":0, 
                "isfamily":0
            }, 
            ....

It photo array goes on for quite a while. How would I get the values of "isPublic"? "Secret" is a String because of the "" and isPublic is an integer or boolean? I basically have to put them into a URL link and download the image.

public class ReadString extends AsyncTask<String, Integer, String>{
@Override
        protected String doInBackground(String... FlickrString) {
            try {
                json = getValue();
                return json.getString(FlickrString[0]);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
@Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            tvTest.setText(result);
        }   

This works for all the strings. How would I get the farm, isfriend, isfamily, and ispublic converted to a string? I tried something else now to get the Int values, I made a whole new class for reading the ints. But it isnt getting the correct int value. I have it download the int and I convert it to a string and then I change a textview to the string of the int using String.valueOf(x); What am i doing wrong?

public class ReadInt extends AsyncTask<String, Integer, String>{

        @Override
        protected String doInBackground(String... FlickrString) {
            try {
                json = getValue();
                int x = json.getInt(FlickrString[0]);
                String y = String.valueOf(x);
                return y;
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            tvTest.setText(result);
        }   
    }   

Thanks!

Upvotes: 0

Views: 5736

Answers (4)

britzl
britzl

Reputation: 10242

I would recommend that you try either Jackson or Gson and convert the JSON into some POJOs. I find that it's easier and less error prone to work with java objects instead of JSONObject, JSONArray and such. Here's an example of how it would work using Jackson. First we create the Objects that map to the JSON data:

public class Photos {

    @JsonProperty
    private int page;

    @JsonProperty
    private int pages;

    ...

    @JsonProperty("photo")
    public List<Photo> photoList;
}

public class Photo {
    @JsonProperty
    private String id;

    @JsonProperty
    private String owner;

    ...

    @JsonProperty("ispublic")
    public boolean isPublic;

    @JsonProperty("isfriend")
    public boolean isFriend;

    @JsonProperty("isfamily")
    public boolean isFamily;
}

Once we've created our pojos we can convert the JSON data into objects using the ObjectMapper. AT least when it comes to Jackson it automatically tries to convert integers to boolean (0 = false, anything else = true) and Strings to booleans ("true" and "false").

ObjectMapper mapper = new ObjectMapper();
Photos photos = mapper.readValue(json, Photos.class);

for(Photo photo : photos.photoList) {
    if(photo.isPublic) {
        // do something
    }
}

PS I've made some of the properties of the pojos public to make the example shorter. You may wish to add getters and keep the properties private.

Upvotes: 0

Srikanth Pai
Srikanth Pai

Reputation: 926

JSONObject jobj = result.getJSONObject("photo");
jobj.getBoolean("ispublic");
jobj.getInt("isfriend");

The variable result is of typeJsonObject that should contain the entire object have pasted above in your code

Upvotes: 2

buptcoder
buptcoder

Reputation: 2702

we assume the jsonString is your json result. You can get the your values like this:

 protected String doInBackground(String... FlickrString) {
            try {
                JSONObject result = new JSONBOject(jsonString);
                JSONObject photots = (JSONObject)result.get("photos");
                JSONArray photosArray = (JSONArray) result.get("photo");
                for(int i = 0; i < photosArray.length(); i++) {
                   JSONObject item = potosArray.get(i);
                   //ispublic
                   int ispublic = item.getInt("ispublic");
                   //isfriend
                   int isfriend = item.getInt("isfriend");
                   //isfamily
                   int isfamily = item.getInt("isfamily");
                }
                json = getValue();
                return json.getString(FlickrString[0]);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

Upvotes: 0

Niko Adrianus Yuwono
Niko Adrianus Yuwono

Reputation: 11122

Java is not like C++.In C++ 0 is false and other integers are true

I think you should create a function like this

private void boolean convertIntToBool(int value) {
   if(value==0) {
     return false;
   } else {
     return true;
   }
}

and use it in the JSON parsing like this for example

boolean isfamily = convertIntToBool(jsonPhotoObject.getInt("isfamily"));

I guess you already know about the JSON parsing so I won't give so much example, but if you have another question about my answer feel free to ask in the comment :)

Upvotes: 1

Related Questions