Skip to content

Instantly share code, notes, and snippets.

@phuong
Last active August 26, 2016 04:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phuong/d16ac22d4bdd03220a84650ee6e7d377 to your computer and use it in GitHub Desktop.
Save phuong/d16ac22d4bdd03220a84650ee6e7d377 to your computer and use it in GitHub Desktop.
/***
* Simple json Library: https://github.com/ralfstx/minimal-json
* javac -cp ".:lib/json-simple-1.1.1.jar" Json.java
* java -cp ".:lib/json-simple-1.1.1.jar" Json
*/
import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.parser.ParseException;
import org.json.simple.parser.JSONParser;
import java.util.Iterator;
import java.util.Map;
class Json {
public static void main(String[] args) {
JSONParser parser = new JSONParser();
String blah = "{\"code\": 200, \"game\": { \"id\": \"BaiCao_1\",\"coinType\":\"normal\",\"countPlayer\":2, \"isPlaying\":true},\"players\":{ \"26\": { \"id\":26, \"realm\": \"Drop\", \"username\": \"Drop\", \"serverId\": \"connector-server-1\", \"joinedRoom\": \"BaiCao\", \"position\":0, \"joinedLogic\":true, \"joinedGame\": \"BaiCao_1\" }, \"27\": { \"id\": 27, \"realm\": \"Drop2\", \"username\": \"Drop2\", \"serverId\": \"connector-server-1\", \"joinedRoom\":\"BaiCao\", \"position\": 1, \"joinedLogic\":true, \"joinedGame\": \"BaiCao_1\" }}}";
try {
Object obj = parser.parse(blah);
JSONObject data = (JSONObject)obj;
JSONObject players = (JSONObject)data.get("players");
Iterator iter = players.entrySet().iterator();
while(iter.hasNext()){
Map.Entry item=(Map.Entry)iter.next();
System.out.println("PlayerId:" + item.getKey());
System.out.println("PlayerData:" + item.getValue());
}
} catch (ParseException pe) {
System.out.println("position: " + pe.getPosition());
System.out.println(pe);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment