JSONObject roomInfoData = JSONObject.fromObject("{'ladder':4,'type':8,'round':10,'playernum':5,'sel1':[1,2,3]}");
com.youchao.pb.CommonPb.Room.Builder rb = com.youchao.pb.CommonPb.Room.newBuilder();
rb.setType(roomInfoData.getInt("type"));
rb.setLimit(1);
rb.setLadder(roomInfoData.getInt("ladder"));
rb.setPlayernum(roomInfoData.getInt("playernum"));
rb.setCreateType(2);
rb.setRound(roomInfoData.getInt("round"));
JSONArray sel1 = roomInfoData.getJSONArray("sel1");
List<Integer> sel = JSONArray.toList(sel1);
Iterator it = sel.iterator();
while(it.hasNext()){
int num = (int) it.next();
System.out.println(num);
com.youchao.pb.CommonPb.Room.Builder rb = com.youchao.pb.CommonPb.Room.newBuilder();
rb.setType(roomInfoData.getInt("type"));
rb.setLimit(1);
rb.setLadder(roomInfoData.getInt("ladder"));
rb.setPlayernum(roomInfoData.getInt("playernum"));
rb.setCreateType(2);
rb.setRound(roomInfoData.getInt("round"));
JSONArray sel1 = roomInfoData.getJSONArray("sel1");
List<Integer> sel = JSONArray.toList(sel1);
Iterator it = sel.iterator();
while(it.hasNext()){
int num = (int) it.next();
System.out.println(num);
}
System.out.println(rb.build().getType());
其中关于类型的转换不需要管,只是拿着要做的事情做了一个测试,发现可以获取到值,重要的是能取得集合。
fromObject转换的是json,更换掉之后,下面的set以及类型都不需要管,直接转集合,循环出来就可以了