【问题标题】:How to parse this JSON file? [duplicate]如何解析这个 JSON 文件? [复制]
【发布时间】:2013-06-27 05:26:12
【问题描述】:

我有以下 JSON 文件:

{
  "data": [
    {
      "creator": 16300398, 
      "description": "Discount txt Discount txt Discount txt\r\nShare\r\n-> Download    the app at http://drinsco.altervista.org/share.php?eid=204142299735343\r\n--> Login in your    Facebook account to use your discount!\r\nasdf asdf asdf asdf asdf asdf asdf asdf asdf asdf    adsf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf    asdf asdf asdf asdfqwer qwer qwer qwer qwer qwer qwer asdf qwer asfdqwerasdf wqer", 
      "end_time": null, 
      "host": "Dave Luke", 
      "location": "Chipotle", 
      "name": "More Test Information", 
      "pic": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-   ash3/174629_204142299735343_644068191_s.jpg", 
      "pic_big": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash3/174629_204142299735343_644068191_n.jpg", 
      "pic_cover": null, 
      "pic_small": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash3/174629_204142299735343_644068191_t.jpg", 
      "pic_square": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash3/174629_204142299735343_644068191_q.jpg", 
      "start_time": "2013-07-11T16:00:00+0200", 
      "timezone": null, 

      "venue": {
        "latitude": 40.720457257566, 
        "longitude": -73.845858172784, 
        "city": "Forest Hills", 
        "state": "NY", 
        "country": "United States", 
        "id": 398225253590019, 
        "street": "70-30 Austin St.", 
        "zip": "11375"
      }
    }
  ]
  }

我需要像这样转换成单个对象:

 public class event {
     public int id;
     public String creator;
     public String description; 
     public String end_time; 
     public String host; 
     public String location; 
     public String name; 
     public String pic; 
     public String pic_big; 
     public String pic_cover; 
     public String pic_small; 
     public String pic_square; 
     public String start_time;
     public String timezone; 
     public String city; 
     public String state; 
     public String country; 
     public String street; 
     public String zip;

感谢 Raghunandan,但我不明白我得到的结果:

private event getevent(String id) {
    // TODO Auto-generated method stub
    final evento e = new evento();
    e.id=id;
    String fqlQuery = "SELECT creator, description ,end_time, host,"
            + "location, name, pic, pic_big, pic_cover, pic_small, pic_square,start_time,"
            + "timezone, venue" + " FROM event WHERE eid=" + id;
    Bundle params = new Bundle();
    params.putString("q", fqlQuery);
    Session session = Session.getActiveSession();
    Request request = new Request(session, "/fql", params, HttpMethod.GET,
            new Request.Callback() {
                public void onCompleted(Response response) {
                     Log.e("", response.toString());
                     String myjsonstring=response.toString();
                     JSONObject jsono = new JSONObject(myjsonstring);
                     JSONArray jsonarray= new JSONArray(jsono.getString("data"));        
                     JSONObject job = (JSONObject) jsonarray.get(0);
                     System.out.println("........."+job.getString("creator"));
                     String description = job.getString("description");
                     String endtime = job.getString("host");
                     String location = job.getString("location");
                     String name = job.getString("name");
                     String pic = job.getString("pic");
                     String picbig = job.getString("pic_big");
                     String piccover = job.getString("pic_cover");
                     String picsmall = job.getString("pic_small");
                     String picsquare = job.getString("pic_square");
                     String starttime = job.getString("start_time");
                     String timezone = job.getString("timezone");
                     System.out.println("........."+job.getString("venue"));
                     JSONObject jb1= new JSONObject(job.getString("venue"));
                     String lati =jb1.getString("latitude");
                     String longi =jb1.getString("longitude");
                     String city =jb1.getString("city");
                     String state =jb1.getString("state");
                     String country =jb1.getString("country");
                     String id =jb1.getString("id");
                     String street =jb1.getString("street");
                     String zip =jb1.getString("zip");
                }
    });

    Request.executeBatchAsync(request);
    return e;

}

这是我得到的完整回复:

06-29 20:48:46.808: E/LISA(10036): {Response:  responseCode: 200, graphObject: GraphObject{graphObjectClass=GraphObject, state={"data":[{"host":"Dave Luke","location":"Chipotle","pic_small":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-ash3\/174629_204142299735343_644068191_t.jpg","pic_cover":null,"pic":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-ash3\/174629_204142299735343_644068191_s.jpg","venue":{"id":398225253590019,"zip":"11375","street":"70-30 Austin St.","state":"NY","longitude":-73.845858172784,"latitude":40.720457257566,"country":"United States","city":"Forest Hills"},"creator":16300398,"timezone":null,"end_time":null,"pic_big":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-ash3\/174629_204142299735343_644068191_n.jpg","description":"Discount txt Discount txt Discount txt\r\nShare\r\n-> Download the app at http:\/\/drinsco.altervista.org\/share.php?eid=204142299735343\r\n--> Login in your Facebook account to use your discount!\r\nasdf asdf asdf asdf asdf asdf asdf asdf asdf asdf adsf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdfqwer qwer qwer qwer qwer qwer qwer asdf qwer asfdqwerasdf wqer","name":"More Test Information","start_time":"2013-07-11T16:00:00+0200","pic_square":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-ash3\/174629_204142299735343_644068191_q.jpg"}]}}, error: null, isFromCache:false}

【问题讨论】:

    标签: android json parsing


    【解决方案1】:

    你可以使用 Gson

    https://code.google.com/p/google-gson/

    教程

    http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html

    或者你可以如下解析json

    JSONObject jsono = new JSONObject(myjsonstring);
    JSONArray jsonarray= new JSONArray(jsono.getString("data")); 
    
    JSONObject job = (JSONObject) jsonarray.get(0);
    String creator = job.getString("creator"));
    // similarly for description and others
    System.out.println("........."+job.getString("venue"));
    JSONObject jb1= new JSONObject(job.getString("venue")); 
    String latitude = jb1.getString("latitude"));
    // similarly for longitude and others. 
    

    编辑:

    JSONObject jsono = new JSONObject(myjsonstring);
    JSONArray jsonarray= new JSONArray(jsono.getString("data"));        
    JSONObject job = (JSONObject) jsonarray.get(0);
    System.out.println("........."+job.getString("creator"));
    String description = job.getString("description");
    String endtime = job.getString("host");
    String location = job.getString("location");
    String name = job.getString("name");
    String pic = job.getString("pic");
    String picbig = job.getString("pic_big");
    String piccover = job.getString("pic_cover");
    String picsmall = job.getString("pic_small");
    String picsquare = job.getString("pic_square");
    String starttime = job.getString("start_time");
    String timezone = job.getString("timezone");
    System.out.println("........."+job.getString("venue"));
    JSONObject jb1= new JSONObject(job.getString("venue"));
    String lati =jb1.getString("latitude");
    String longi =jb1.getString("longitude");
    String city =jb1.getString("city");
    String state =jb1.getString("state");
    String country =jb1.getString("country");
    String id =jb1.getString("id");
    String street =jb1.getString("street");
    String zip =jb1.getString("zip");
    

    【讨论】:

    • 请我确定这指向正确的方向,您能否详细说明一下?谢谢!!!!!!
    • @LisaAnne 告诉我应该详细说明什么?
    • Raghunandan 它不工作,不幸的是......
    • 什么不适合你?它工作正常。
    • @LisaAnne 告诉我什么不起作用?
    【解决方案2】:

    您可以使用Google-Gson

    看看他们的例子here。也许你可以在那里找到有用的东西。

    【讨论】:

      猜你喜欢
      • 2020-03-19
      • 1970-01-01
      • 2016-01-28
      • 2023-03-03
      • 1970-01-01
      • 2012-05-24
      • 2014-03-20
      • 2020-08-10
      • 2013-07-16
      相关资源
      最近更新 更多