package JsonTest;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
public class JsonTest {
public static void main(String[] args) throws JSONException {
String json = "{\"name\":\"reiz\"}";
JSONObject jsonObj = new JSONObject(json);
String name = jsonObj.getString("name");
jsonObj.put("initial", name.substring(0, 1).toUpperCase());
String[] likes = new String[] { "JavaScript", "Skiing", "Apple Pie" };
jsonObj.put("likes", likes);
Map<String, String> ingredients = new HashMap<String, String>();
ingredients.put("apples", "3kg");
ingredients.put("sugar", "1kg");
ingredients.put("pastry", "2.4kg");
ingredients.put("bestEaten", "outdoors");
jsonObj.put("ingredients", ingredients);
System.out.println(jsonObj);
}
}
package JsonTest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class JsonRead {
public static void main(String[] args) throws JSONException {
String json = "{\"results\":[{\"to_user_id\":1728647563,"
+ "\"to_user\":\"kiroro恋\"},{\"to_user_id\":728647563,"
+ "\"to_user\":\"恋\"}]}";
JSONObject jsonObj = new JSONObject(json);
JSONArray j = jsonObj.getJSONArray("results");
System.out.println(j.getJSONObject(0).getString("to_user_id"));
System.out.println(j.get(0));
System.out.println(j.getJSONObject(1).getString("to_user_id"));
System.out.println(j.get(1));
// 1728647563
// {"to_user_id":1728647563,"to_user":"kiroro恋"}
// 728647563
// {"to_user_id":728647563,"to_user":"恋"}
}
}
//{
// "results":[
// {
// "to_user_id":1728647563,
// "to_user":"kiroro恋",
// "text":"喜欢坐在你的车里听王若琳的歌",
// "id":4192745203,
// "from_user_id":1728647563,
// "from_user":"kiroro恋",
// "iso_language_code":"nl",
// "source":"<a href=\"http://t.sina.com.cn\" rel=\"nofollow\">新浪微博</a>",
// "profile_image_url":"http://tp4.sinaimg.cn/1728647563/50/1289280876/0",
// "created_at":"Thu Dec 09 13:57:20 +0800 2010"
// },
// ...
// ],
// "since_id":0,
// "max_id":4192745203,
// "results_per_page":0,
// "next_page":"?page=2&max_id=1291869235&q=王若琳",
// "refresh_url":"?since_id=1291874240&q=王若琳",
// "completed_in":0.24699999392032623,
// "page":1,"query":"王若琳"
//}