2013-06-29 391 views
0

我有以下的JSON文件:如何解析這個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,但我不明白,結果我得到:

enter image description here

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; 

} 

這是comoplete響應我得到:

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} 

回答

3

您可以使用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"); 
+0

請確信這是指向正確的方向,請您詳細說明一下?謝謝!!!!! –

+0

@LisaAnne告訴我我應該詳細說明一下嗎? – Raghunandan

+0

Raghunandan它不工作,不幸... –