0

我有以下 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}
4

2 回答 2

3

You can use Gson

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

Tutorial

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

Or You can parse the json as below

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. 

Edit:

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");
于 2013-06-29T15:28:27.597 回答
2

您可以使用Google-Gson

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

于 2013-06-29T15:18:17.010 回答