Created
July 5, 2011 03:24
-
-
Save nijikokun/1064213 to your computer and use it in GitHub Desktop.
Json Simple Wrapper \o/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import org.json.simple.JSONArray; | |
import org.json.simple.JSONObject; | |
import org.json.simple.parser.JSONParser; | |
import org.json.simple.parser.ParseException; | |
/** | |
* json_simple wrapper by Nijikokun (c) 2011 | |
*/ | |
public class JSON { | |
private String source; | |
private JSONArray data; | |
private JSONParser parser; | |
public JSON() { | |
this.parser = new JSONParser(); | |
} | |
public JSON setSource(String source) { | |
this.source = source; | |
return this; | |
} | |
public Object parse() throws ParseException { | |
return parse(this.source); | |
} | |
final public Object parse(String source) throws ParseException { | |
return this.parser.parse(source); | |
} | |
final public JSONArray getArray(Object value) { | |
return (JSONArray)value; | |
} | |
final public JSONArray getArray(JSONArray array, int value) { | |
return getArray(array.get(value)); | |
} | |
final public String[] getStringArray(JSONArray obj, int value) { | |
Object[] array = (getArray(obj.get(value))).toArray(); | |
return Arrays.copyOf(array, array.length, String[].class); | |
} | |
final public JSONArray getArray(JSONObject array, String value) { | |
return getArray(array.get(value)); | |
} | |
final public int getInteger(JSONObject obj, String value) { | |
return Integer.valueOf(String.valueOf(obj.get(value))); | |
} | |
final public String getString(JSONObject obj, String value) { | |
return String.valueOf(obj.get(value)); | |
} | |
final public String[] getStringArray(JSONObject obj, String value) { | |
Object[] array = (getArray(obj.get(value))).toArray(); | |
return Arrays.copyOf(array, array.length, String[].class); | |
} | |
final public JSONObject getObject(Object value) { | |
return (JSONObject)value; | |
} | |
final public JSONObject getObject(JSONArray array, int value) { | |
return getObject(array.get(value)); | |
} | |
final public JSONObject getObject(JSONObject array, String value) { | |
return getObject(array.get(value)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment