1

I am trying to deserialize the following string, I am somewhat new to java and I cannot get this to work for the life of me... I am only trying to decode two strings in the object for now. My JSON and Java classes below. I am getting the result variable ok.

{
   "result": "true",
   "recentlyMarkedTerritories": {
      "0": {
         "pk_activity": "471",
         "fk_activity_type": "100",
         "activity_content": "Hhhhh",
         "fk_user": "2",
         "activity_image": "2_QZe73f4t8s3R1317230457.jpg",
         "created": "1317244857",
         "activity_status": "1",
         "activity_location_lat": "43.515283",
         "activity_location_lon": "-79.880678",
         "fk_walk": null,
         "fk_event_location": "73",
         "user_point": "0",
         "public_image": "0",
         "fk_event_location_lat": "43.515273",
         "fk_event_location_lon": "-79.879989",
         "profile_image": "2_y9JlkI3CZDml1312492743.jpg",
         "user_gender": "1",
         "user_dob": "236073600",
         "user_nickname": "junoman",
         "isFriend": "false",
         "profile_image_thumb": "2_y9JlkI3CZDml1312492743_t.jpg",
         "activity_image_thumb": "2_QZe73f4t8s3R1317230457_t.jpg",
         "relationship_status_idx": "2",
         "isBlocked": "false"
      },
      "1": {
         "pk_activity": "469",
         "fk_activity_type": "100",
         "activity_content": "Jsjsjs",
         "fk_user": "1",
         "activity_image": null,
         "created": "1317244508",
         "activity_status": "1",
         "activity_location_lat": "43.515283",
         "activity_location_lon": "-79.880678",
         "fk_walk": null,
         "fk_event_location": "73",
         "user_point": "0",
         "public_image": "0",
         "fk_event_location_lat": "43.515273",
         "fk_event_location_lon": "-79.879989",
         "profile_image": "1_4Cpkofueqnrb1316895161.jpg",
         "user_gender": "1",
         "user_dob": "116841600",
         "user_nickname": "JoePennington",
         "isFriend": "false",
         "profile_image_thumb": "1_4Cpkofueqnrb1316895161_t.jpg",
         "activity_image_thumb": null,
         "relationship_status_idx": "1",
         "isBlocked": "false"
      },
      .....
   }
}

And my java class below

RecentActivity infoList = null;
Gson gson = new Gson();
infoList = gson.fromJson(JSONString, RecentActivity.class);

public class RecentActivity {
    String result;
    recentlyMarkedTerritories recentlyMarkedTerritories = null;

    public RecentActivity() {

    }

    public class recentlyMarkedTerritories {
        public Set<recentlyMarkedTerritories> pk_activity = new HashSet<recentlyMarkedTerritories>() ;

        public recentlyMarkedTerritories() {    }
    }
}

Please forgive my lack of description but I'm sure the code helps. The JSON is already used in other applications so changing it is not an option.. :(

Thanks!

2 Answers2

1

Here are some nice Tutorials for JSON that will help you out.

GSON

JSON

JSON Example with source code

UPDATED

Try like this,

 try {
            JSONObject object = new JSONObject(jsonString);
            JSONObject myObject = object.getJSONObject("recentlyMarkedTerritories");

            for (int i = 0; i < object.length(); i++) {
                JSONObject myObject2 = myObject.getJSONObject(Integer.toString(i));
                System.out.println(myObject2.toString(2));  
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • I have been looking all over these and I cant find anything that explains how to name or iterate through objects with numeric names like in the json above.. Almost seems not achievable in java? – Daniel Kelly Oct 06 '11 at 23:27
  • Is this your complete JSON String...? – Lalit Poptani Oct 07 '11 at 02:34
  • No it is not the complete string.. Sorry, I sort of hid the "...." after the second entry. The list may go to 5 or maybe 100. I accepted your answer because I found it much easier to handle the native Android JSONObject than the Google GSONObject and am now using that. Thanks for the great links and shove in the right direction! – Daniel Kelly Oct 13 '11 at 22:03
0

I am not sure of the gson code to write, but the structure of your json looks more like the following java representation (though you might want booleans and ints instead of String fields):

public class RecentActivity {
    String result;
    Map<String,RecentlyMarkedTerritory> recentlyMarkedTerritories = null;
}

public class RecentlyMarkedTerritory {
    String pk_activity;
    // other fields
}
Stephen Denne
  • 36,219
  • 10
  • 45
  • 60
  • Hmmmm this doesn't quite work but maybe you can explain or hint at this.. What exactly do the < > mean and what is that called? I can find enough info to understand with the right terms :) – Daniel Kelly Oct 06 '11 at 23:30
  • @Daniel the < > are using what is called `Generics` and here they mean that the Map is a map of Strings to RecentlyMarkedTerritory objects. The Strings would be "0", "1", etc. – Stephen Denne Oct 09 '11 at 07:28