JSON对象的Java重新presentation [英] Java representation of JSON Object

查看:132
本文介绍了JSON对象的Java重新presentation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想反序列化下面的字符串,我有点新的Java和我不能让这对我的生活工作...我只是想取消code两个字符串的对象,现在。我的JSON及以下Java类。我得到的结果变量ok了。

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"
      },
      .....
   }
}

和下面

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() {    }
    }
}

请原谅我缺乏的描述,但我敢肯定,code有助于。该JSON是在其他应用程序已经使用,因此更改它不是一个选项..:(

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.. :(

谢谢!

推荐答案

下面是JSON一些很好的教程,这将帮助你。

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

GSON

JSON

JSON与源$ C ​​$ C 示例

更新时间:

试试这个样子,

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

这篇关于JSON对象的Java重新presentation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆