Java representation of JSON Object

*爱你&永不变心* 提交于 2019-12-20 04:55:13

问题


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!


回答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();
        }



回答2:


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
}


来源:https://stackoverflow.com/questions/7669730/java-representation-of-json-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!