形式一:
1 2 3 4 5 6 7 8 9 10 11 12 |
import net.sf.json.JSONObject; String json = "{\"name\":\"lss\"}"; JSONObject jsonObj = JSONObject.fromObject(json); String name = jsonObj.getString("name"); Iterator it = jsonObj.keys(); List<String> keyListstr = new ArrayList<String>(); while(it.hasNext()){ key = (String) it.next(); value = jsonObject.getString(key); } |
形式二(类似:{“info”:[{“goodsId”:”1234″,”goodsq”:”10″},{“goodsId”:”5678″,”goodsq”:”20″}]}):
1 2 3 4 5 6 7 8 |
JSONObject jsonObject = new JSONObject(jsonString); JSONArray jsonArray =jsonObject.getJSONArray(“info”); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jo = jsonArray.getJSONObject(i); System.out.println(jo.getString("goodsld")); System.out.println(jo.getString("goodsq")); } |