我正在尝试反序列化以下字符串,对于Java来说我是新手,我无法在我的一生中使用它…我现在仅尝试在对象中解码两个字符串.我的JSON和Java类如下.我得到的结果变量确定.
{
"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"
},
.....
}
}
还有我下面的java类
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() { }
}
}
请原谅我缺乏描述,但我确信代码会有所帮助. JSON已在其他应用程序中使用,因此不可更改.
谢谢!
解决方法:
这是一些不错的JSON教程,可以帮助您.
更新
这样尝试
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();
}