Java简单的解析Json
package jsoner;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class jsoner {
/** * @param args */
public static void main(String[] args) {
// TODO 自动生成的方法存根
json_generate();
json_anal();
}
/*生成JSON格式*/
public static void json_generate() throws JSONException{
JSONObject jsonobject = new JSONObject();
JSONObject jsonobjects = new JSONObject();
Object object = null;
try {
jsonobject.put("ww", "好饿啊");
jsonobject.put("cc", "好累啊");
jsonobjects.put("tip", 1);
jsonobjects.put("data",jsonobject);
}catch(JSONException e) {
e.printStackTrace();
}
System.out.println(jsonobjects.toString());
}
/*解析JSON格式*/
public static void json_anal() throws JSONException{
String json = "{tip:1,data:[{ww:'好累啊',ac:'好饿啊'},{ww:'好蠢啊',ac:'好无奈啊'},{ww:'啊啊啊啊',ac:'好无力啊'}]}";
JSONObject jsonobject = new JSONObject(json);
try {
JSONArray array = jsonobject.getJSONArray("data");
//取出JSON数组
for(int i = 0;i<array.length();i++) {
JSONObject lan = array.getJSONObject(i); System.out.println(lan.getString("ww") + " " + lan.getString("ac"));
//取出字符串
}
}catch(JSONException e) {
e.printStackTrace();
}
}
}
附上第三方库下载链接:http://mvnrepository.com/artifact/org.json/json