2012-07-12 38 views
1

我想要做的是创建一个JSONObject,其中包含由String组织的其他JSONObjects数组的JSONObject?例如,我想创建一个包含包含灯具对象的数组,也可以通过比赛日期组织的一个JSONObjectAndroid将Json对象添加到数组中

给你什么,我想acheive

JSONObject (JSONArray("Object 10/10/12" {(fixtures content)(fixtures content)}") 
        ("Object 11/10/12" {(fixtures content)(fixtures content)}")) 

视觉参考继承人什么到目前为止,我已经尝试过,但就是无法得到它的工作

 String matchDate1 = null; 
     JSONArray datesArray = null; 
     JSONObject fixturesInfo = null; 
     JSONArray fixturesInfoArray = null; 
     String matchDateTemp = null; 

     for(int f = 0; f < fixturesArray.length(); f++){ 

      JSONObject matchDateDict = fixturesArray.getJSONObject(f); 
      matchDate1 = matchDateDict.getString("matchdate"); 
      JSONArray fixturesInfoDict = fixturesInfo.getJSONArray(matchDate1); 

      if(fixturesInfoDict == null){ 
       tempArray = null; 
      } else { 
       tempArray = fixturesInfoDict; 
      } 

      if(matchDateTemp != matchDate1){ 
       fixturesInfoArray.put(matchDate1); 
      } 

      matchDateTemp = matchDate1; 

      tempArray.put(fixturesArray.getJSONObject(f)); 
      fixturesInfo.put(matchDate1, tempArray); 

     } 




      Log.v("MyFix", "fixturesInfo = " + fixturesInfo); 

     }catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

继承人的JSON提要

{ 
    "code": 200, 
    "error": null, 
    "data": { 
     "fixtures": [{ 
      "kickoff": "15:00:00", 
      "matchdate": "2012-07-14", 
      "homescore": null, 
      "awayscore": null, 
      "attendance": null, 
      "homepens": null, 
      "awaypens": null, 
      "division_id": "5059", 
      "division": "Testing 1", 
      "comp": "LGE", 
      "location": null, 
      "fixture_note": null, 
      "hometeam_id": "64930", 
      "hometeam": "Team 1", 
      "awayteam_id": "64933", 
      "awayteam": "Team 4" 
     }, { 
      "kickoff": "15:00:00", 
      "matchdate": "2012-07-14", 
      "homescore": null, 
      "awayscore": null, 
      "attendance": null, 
      "homepens": null, 
      "awaypens": null, 
      "division_id": "5059", 
      "division": "Testing 1", 
      "comp": "LGE", 
      "location": null, 
      "fixture_note": null, 
      "hometeam_id": "64935", 
      "hometeam": "Team 6", 
      "awayteam_id": "64937", 
      "awayteam": "Team 8" 
     }, { 
      "kickoff": "15:00:00", 
      "matchdate": "2012-07-28", 
      "homescore": null, 
      "awayscore": null, 
      "attendance": null, 
      "homepens": null, 
      "awaypens": null, 
      "division_id": "5059", 
      "division": "Testing 1", 
      "comp": "LGE", 
      "location": null, 
      "fixture_note": null, 
      "hometeam_id": "64930", 
      "hometeam": "Team 1", 
      "awayteam_id": "64931", 
      "awayteam": "Team 2" 
     }, { 
      "kickoff": "15:00:00", 
      "matchdate": "2012-07-28", 
      "homescore": null, 
      "awayscore": null, 
      "attendance": null, 
      "homepens": null, 
      "awaypens": null, 
      "division_id": "5059", 
      "division": "Testing 1", 
      "comp": "LGE", 
      "location": null, 
      "fixture_note": null, 
      "hometeam_id": "64930", 
      "hometeam": "Team 1", 
      "awayteam_id": "64931", 
      "awayteam": "Team 2" 
     }] 
    } 
} 

回答

12

从你说的话来看,你似乎试图从JSONObjects中建立一个JSONArray。这可能有所帮助:

public void writeJSON() { 
    JSONObject user = new JSONObject(); 
    JSONObject user2; 
    user2 = new JSONObject(); 
    try { 
     user.put("dish_id", "1"); 
     user.put("dish_custom", "2"); 
     user.put("quantity", "2"); 
     user.put("shared", "2"); 

     user2.put("dish_id", "2"); 
     user2.put("dish_custom", "2"); 
     user2.put("quantity", "4"); 
     user2.put("shared", "3"); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    JSONArray notebookUsers = new JSONArray(); 
    notebookUsers.put(user); 
    notebookUsers.put(user2); 
    System.out.println("the JSON ARRAY is"+notebookUsers); 

这里2个json对象被添加到1个JSONArray。

System.out.println的输出将是这个样子:

the JSON ARRAY is[{"shared":"2","dish_custom":"2","dish_id":"1","quantity":"2"},{"shared":"3","dish_custom":"2","dish_id":"2","quantity":"4"}] 

而你正在使用的字符串比较是不正确的。

if(matchDateTemp != matchDate1) 

你不能比较那样的字符串。你可以使用类似的东西:

if(!(matchDateTemp.equals(matchDate1))) 
0

解析JSON的最好的事情是使用GSON。你所要做的就是

  • 下载GSON罐子,添加到您的构建路径
  • 现在创建表示JSON数据结构
  • 解析中只有一行数据类。 GSON会为你做

在短短的一行解析现在

String json = "{\"code\":200,\"error\":null,\"data\":{\"fixtures\":[{\"kickoff\":\"15:00:00\",\"matchdate\":\"2012-07-14\",\"homescore\":null,\"awayscore\":null,\"attendance\":null,\"homepens\":null,\"awaypens\":null,\"division_id\":\"5059\",\"division\":\"Testing 1\",\"comp\":\"LGE\",\"location\":null,\"fixture_note\":null,\"hometeam_id\":\"64930\",\"hometeam\":\"Team 1\",\"awayteam_id\":\"64933\",\"awayteam\":\"Team 4\"},{\"kickoff\":\"15:00:00\",\"matchdate\":\"2012-07-14\",\"homescore\":null,\"awayscore\":null,\"attendance\":null,\"homepens\":null,\"awaypens\":null,\"division_id\":\"5059\",\"division\":\"Testing 1\",\"comp\":\"LGE\",\"location\":null,\"fixture_note\":null,\"hometeam_id\":\"64935\",\"hometeam\":\"Team 6\",\"awayteam_id\":\"64937\",\"awayteam\":\"Team 8\"},{\"kickoff\":\"15:00:00\",\"matchdate\":\"2012-07-28\",\"homescore\":null,\"awayscore\":null,\"attendance\":null,\"homepens\":null,\"awaypens\":null,\"division_id\":\"5059\",\"division\":\"Testing 1\",\"comp\":\"LGE\",\"location\":null,\"fixture_note\":null,\"hometeam_id\":\"64930\",\"hometeam\":\"Team 1\",\"awayteam_id\":\"64931\",\"awayteam\":\"Team 2\"},{\"kickoff\":\"15:00:00\",\"matchdate\":\"2012-07-28\",\"homescore\":null,\"awayscore\":null,\"attendance\":null,\"homepens\":null,\"awaypens\":null,\"division_id\":\"5059\",\"division\":\"Testing 1\",\"comp\":\"LGE\",\"location\":null,\"fixture_note\":null,\"hometeam_id\":\"64930\",\"hometeam\":\"Team 1\",\"awayteam_id\":\"64931\",\"awayteam\":\"Team 2\"}]}}"; 

MatchDetails matchDetails=new Gson().fromJson(json,MatchDetails.class); 
System.out.println(matchDetails); 
  • MatchDetails类有所有FixtureLists,你需要通过matchDetails.getData().getFixtures();
  • 访问夹具阵列

    MatchDetails class

    public class MatchDetails { 
    
        //"code":200,"error":null 
    
        private String code; 
        private String error; 
        private Fixtures data; 
        /** 
        * Gets the code. 
        * 
        * @return <tt> the code.</tt> 
        */ 
        public String getCode() { 
         return code; 
        } 
        /** 
        * Sets the code. 
        * 
        * @param code <tt> the code to set.</tt> 
        */ 
        public void setCode(String code) { 
         this.code = code; 
        } 
        /** 
        * Gets the error. 
        * 
        * @return <tt> the error.</tt> 
        */ 
        public String getError() { 
         return error; 
        } 
        /** 
        * Sets the error. 
        * 
        * @param error <tt> the error to set.</tt> 
        */ 
        public void setError(String error) { 
         this.error = error; 
        } 
        /** 
        * Gets the data. 
        * 
        * @return <tt> the data.</tt> 
        */ 
        public Fixtures getData() { 
         return data; 
        } 
        /** 
        * Sets the data. 
        * 
        * @param data <tt> the data to set.</tt> 
        */ 
        public void setData(Fixtures data) { 
         this.data = data; 
        } 
        /* (non-Javadoc) 
        * @see java.lang.Object#toString() 
        */ 
        @Override 
        public String toString() { 
         return "MatchDetails [code=" + code + ", error=" + error + ", data=" 
           + data + "]"; 
        } 
    
    
    } 
    

    EDIT 编辑FixturessortFixtures功能。使用它,如下

    String json = "{\"code\":200,\"error\":null,\"data\":{\"fixtures\":[{\"kickoff\":\"15:00:00\",\"matchdate\":\"2012-05-14\",\"homescore\":null,\"awayscore\":null,\"attendance\":null,\"homepens\":null,\"awaypens\":null,\"division_id\":\"5059\",\"division\":\"Testing 1\",\"comp\":\"LGE\",\"location\":null,\"fixture_note\":null,\"hometeam_id\":\"64930\",\"hometeam\":\"Team 1\",\"awayteam_id\":\"64933\",\"awayteam\":\"Team 4\"},{\"kickoff\":\"15:00:00\",\"matchdate\":\"2012-07-14\",\"homescore\":null,\"awayscore\":null,\"attendance\":null,\"homepens\":null,\"awaypens\":null,\"division_id\":\"5059\",\"division\":\"Testing 1\",\"comp\":\"LGE\",\"location\":null,\"fixture_note\":null,\"hometeam_id\":\"64935\",\"hometeam\":\"Team 6\",\"awayteam_id\":\"64937\",\"awayteam\":\"Team 8\"},{\"kickoff\":\"15:00:00\",\"matchdate\":\"2012-12-28\",\"homescore\":null,\"awayscore\":null,\"attendance\":null,\"homepens\":null,\"awaypens\":null,\"division_id\":\"5059\",\"division\":\"Testing 1\",\"comp\":\"LGE\",\"location\":null,\"fixture_note\":null,\"hometeam_id\":\"64930\",\"hometeam\":\"Team 1\",\"awayteam_id\":\"64931\",\"awayteam\":\"Team 2\"},{\"kickoff\":\"15:00:00\",\"matchdate\":\"2012-01-28\",\"homescore\":null,\"awayscore\":null,\"attendance\":null,\"homepens\":null,\"awaypens\":null,\"division_id\":\"5059\",\"division\":\"Testing 1\",\"comp\":\"LGE\",\"location\":null,\"fixture_note\":null,\"hometeam_id\":\"64930\",\"hometeam\":\"Team 1\",\"awayteam_id\":\"64931\",\"awayteam\":\"Team 2\"}]}}"; 
    
    MatchDetails matchDetails=new Gson().fromJson(json,MatchDetails.class); 
    System.out.println(matchDetails); 
    
    System.out.println("Unsorted"); 
    
    Fixtures fixturesObj = matchDetails.getData(); 
    List<Fixture> fixtures = fixturesObj.getFixtures(); 
    for (Fixture fixture : fixtures) { 
        System.out.println(fixture); 
    } 
    
    System.out.println("Sorted"); 
    
    fixtures = fixturesObj.sortFixtures(); 
    for (Fixture fixture : fixtures) { 
        System.out.println(fixture); 
    } 
    

    Fixtures

    import java.text.ParseException; 
    import java.text.SimpleDateFormat; 
    import java.util.Collections; 
    import java.util.Comparator; 
    import java.util.Date; 
    import java.util.List; 
    
    /** 
    * <br> 
    * <div style="width:600px;text-align:justify;"> 
    * 
    * TODO: Class comment. 
    * 
    * </div> 
    * <br> 
    * @author Sunil Kumar E 
    * 
    */ 
    public class Fixtures { 
    
        private List<Fixture> fixtures; 
    
        /** 
        * Gets the fixtures. 
        * 
        * @return <tt> the fixtures.</tt> 
        */ 
        public List<Fixture> getFixtures() { 
         return fixtures; 
        } 
    
        /** 
        * Sets the fixtures. 
        * 
        * @param fixtures <tt> the fixtures to set.</tt> 
        */ 
        public void setFixtures(List<Fixture> fixtures) { 
         this.fixtures = fixtures; 
        } 
    
        /* (non-Javadoc) 
        * @see java.lang.Object#toString() 
        */ 
        @Override 
        public String toString() { 
         return "Fixtures [fixtures=" + fixtures + "]"; 
        } 
    
    
        public List<Fixture> sortFixtures() 
        { 
         Collections.sort(getFixtures(), new Comparator<Fixture>() { 
    
          @Override 
          public int compare(Fixture o1, Fixture o2) { 
           SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd"); 
    
           Date date1 = null; 
           Date date2 = null; 
    
           try { 
            date1 = df1 .parse(o1.getMatchdate()); 
            date2 = df1 .parse(o2.getMatchdate()); 
           } catch (ParseException e) { 
            e.printStackTrace(); 
           } 
           if(date1==null || date2==null) return 0; 
    
           if (date1.after(date2)) 
            return 1; 
           else if(date1.before(date2)) 
            return -1; 
           return 0; 
          } 
         }); 
    
         return getFixtures(); 
        } 
    } 
    

    Fixture

    public class Fixture { 
    //"kickoff":"15:00:00","matchdate":"2012-07-14","homescore":null, 
    // "awayscore":null,"attendance":null,"homepens":null,"awaypens":null, 
    // "division_id":"5059","division":"Testing 1","comp":"LGE","location":null, 
    // "fixture_note":null,"hometeam_id":"64930","hometeam":"Team 1", 
    // "awayteam_id":"64933","awayteam":"Team 4" 
    
        private String kickoff; 
        private String matchdate; 
        private String homescore; 
        private String awayscore; 
        private String attendance; 
        private String homepens; 
        private String awaypens; 
        private String division_id; 
        private String division; 
        private String comp; 
        private String location; 
        private String fixture_note; 
        private String hometeam_id; 
        private String hometeam; 
        private String awayteam_id; 
        private String awayteam; 
        /** 
        * Gets the kickoff. 
        * 
        * @return <tt> the kickoff.</tt> 
        */ 
        public String getKickoff() { 
         return kickoff; 
        } 
        /** 
        * Sets the kickoff. 
        * 
        * @param kickoff <tt> the kickoff to set.</tt> 
        */ 
        public void setKickoff(String kickoff) { 
         this.kickoff = kickoff; 
        } 
        /** 
        * Gets the matchdate. 
        * 
        * @return <tt> the matchdate.</tt> 
        */ 
        public String getMatchdate() { 
         return matchdate; 
        } 
        /** 
        * Sets the matchdate. 
        * 
        * @param matchdate <tt> the matchdate to set.</tt> 
        */ 
        public void setMatchdate(String matchdate) { 
         this.matchdate = matchdate; 
        } 
        /** 
        * Gets the homescore. 
        * 
        * @return <tt> the homescore.</tt> 
        */ 
        public String getHomescore() { 
         return homescore; 
        } 
        /** 
        * Sets the homescore. 
        * 
        * @param homescore <tt> the homescore to set.</tt> 
        */ 
        public void setHomescore(String homescore) { 
         this.homescore = homescore; 
        } 
        /** 
        * Gets the awayscore. 
        * 
        * @return <tt> the awayscore.</tt> 
        */ 
        public String getAwayscore() { 
         return awayscore; 
        } 
        /** 
        * Sets the awayscore. 
        * 
        * @param awayscore <tt> the awayscore to set.</tt> 
        */ 
        public void setAwayscore(String awayscore) { 
         this.awayscore = awayscore; 
        } 
        /** 
        * Gets the attendance. 
        * 
        * @return <tt> the attendance.</tt> 
        */ 
        public String getAttendance() { 
         return attendance; 
        } 
        /** 
        * Sets the attendance. 
        * 
        * @param attendance <tt> the attendance to set.</tt> 
        */ 
        public void setAttendance(String attendance) { 
         this.attendance = attendance; 
        } 
        /** 
        * Gets the homepens. 
        * 
        * @return <tt> the homepens.</tt> 
        */ 
        public String getHomepens() { 
         return homepens; 
        } 
        /** 
        * Sets the homepens. 
        * 
        * @param homepens <tt> the homepens to set.</tt> 
        */ 
        public void setHomepens(String homepens) { 
         this.homepens = homepens; 
        } 
        /** 
        * Gets the awaypens. 
        * 
        * @return <tt> the awaypens.</tt> 
        */ 
        public String getAwaypens() { 
         return awaypens; 
        } 
        /** 
        * Sets the awaypens. 
        * 
        * @param awaypens <tt> the awaypens to set.</tt> 
        */ 
        public void setAwaypens(String awaypens) { 
         this.awaypens = awaypens; 
        } 
        /** 
        * Gets the division_id. 
        * 
        * @return <tt> the division_id.</tt> 
        */ 
        public String getDivision_id() { 
         return division_id; 
        } 
        /** 
        * Sets the division_id. 
        * 
        * @param division_id <tt> the division_id to set.</tt> 
        */ 
        public void setDivision_id(String division_id) { 
         this.division_id = division_id; 
        } 
        /** 
        * Gets the division. 
        * 
        * @return <tt> the division.</tt> 
        */ 
        public String getDivision() { 
         return division; 
        } 
        /** 
        * Sets the division. 
        * 
        * @param division <tt> the division to set.</tt> 
        */ 
        public void setDivision(String division) { 
         this.division = division; 
        } 
        /** 
        * Gets the comp. 
        * 
        * @return <tt> the comp.</tt> 
        */ 
        public String getComp() { 
         return comp; 
        } 
        /** 
        * Sets the comp. 
        * 
        * @param comp <tt> the comp to set.</tt> 
        */ 
        public void setComp(String comp) { 
         this.comp = comp; 
        } 
        /** 
        * Gets the location. 
        * 
        * @return <tt> the location.</tt> 
        */ 
        public String getLocation() { 
         return location; 
        } 
        /** 
        * Sets the location. 
        * 
        * @param location <tt> the location to set.</tt> 
        */ 
        public void setLocation(String location) { 
         this.location = location; 
        } 
        /** 
        * Gets the fixture_note. 
        * 
        * @return <tt> the fixture_note.</tt> 
        */ 
        public String getFixture_note() { 
         return fixture_note; 
        } 
        /** 
        * Sets the fixture_note. 
        * 
        * @param fixture_note <tt> the fixture_note to set.</tt> 
        */ 
        public void setFixture_note(String fixture_note) { 
         this.fixture_note = fixture_note; 
        } 
        /** 
        * Gets the hometeam_id. 
        * 
        * @return <tt> the hometeam_id.</tt> 
        */ 
        public String getHometeam_id() { 
         return hometeam_id; 
        } 
        /** 
        * Sets the hometeam_id. 
        * 
        * @param hometeam_id <tt> the hometeam_id to set.</tt> 
        */ 
        public void setHometeam_id(String hometeam_id) { 
         this.hometeam_id = hometeam_id; 
        } 
        /** 
        * Gets the hometeam. 
        * 
        * @return <tt> the hometeam.</tt> 
        */ 
        public String getHometeam() { 
         return hometeam; 
        } 
        /** 
        * Sets the hometeam. 
        * 
        * @param hometeam <tt> the hometeam to set.</tt> 
        */ 
        public void setHometeam(String hometeam) { 
         this.hometeam = hometeam; 
        } 
        /** 
        * Gets the awayteam_id. 
        * 
        * @return <tt> the awayteam_id.</tt> 
        */ 
        public String getAwayteam_id() { 
         return awayteam_id; 
        } 
        /** 
        * Sets the awayteam_id. 
        * 
        * @param awayteam_id <tt> the awayteam_id to set.</tt> 
        */ 
        public void setAwayteam_id(String awayteam_id) { 
         this.awayteam_id = awayteam_id; 
        } 
        /** 
        * Gets the awayteam. 
        * 
        * @return <tt> the awayteam.</tt> 
        */ 
        public String getAwayteam() { 
         return awayteam; 
        } 
        /** 
        * Sets the awayteam. 
        * 
        * @param awayteam <tt> the awayteam to set.</tt> 
        */ 
        public void setAwayteam(String awayteam) { 
         this.awayteam = awayteam; 
        } 
        /* (non-Javadoc) 
        * @see java.lang.Object#toString() 
        */ 
        @Override 
        public String toString() { 
         return "Fixture [kickoff=" + kickoff + ", matchdate=" + matchdate 
           + ", homescore=" + homescore + ", awayscore=" + awayscore 
           + ", attendance=" + attendance + ", homepens=" + homepens 
           + ", awaypens=" + awaypens + ", division_id=" + division_id 
           + ", division=" + division + ", comp=" + comp + ", location=" 
           + location + ", fixture_note=" + fixture_note 
           + ", hometeam_id=" + hometeam_id + ", hometeam=" + hometeam 
           + ", awayteam_id=" + awayteam_id + ", awayteam=" + awayteam 
           + "]"; 
        } 
    } 
    
  • +0

    我看不到在你的代码的部分地方按日期排序灯具。 – Squonk 2012-07-12 11:10:32

    +0

    你有所有的灯具存储在列表中,并且可以应用任何日期排序逻辑来对灯具进行排序 – sunil 2012-07-12 11:21:36

    +0

    @Squonk已添加排序灯具的功能 – sunil 2012-07-12 11:42:17