java 解析 json

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":"王若琳"
//}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值