json的初步学习
2015-08-10 19:11
651 查看
1、JSON:一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.可以在不同平台间进行数据交换。JSON采用兼容性很高的文本格式,同时也具备类似于C语言体系的行为。
2、JSON的规则很简单:对象是一个无序的“‘名称/值’对”集合.映射的集合(对象)用大括号(“{}”)表示,并列数据的集合(数组)用方括号(“[]”)表示.
3、元素值可具有的类型:string, number, object, array, true, false, null
4、组合的例子:[
{ "name": "张三", "age":22, "email": "zhangsan@qq.com" },
{ "name": "李四", "age":23, "email": "lisi@qq.com"},
{ "name": "王五", "age":24, "email": "wangwu@qq.com" }
]
解析步骤
1、读取html文件源代码,获取一个json字符串
String in = {"weatherinfo":{"city":"北京","cityid":"101010100","temp":"21","WD":"东南风","WS":"2级","SD":"78%","WSE":"2","time":"21:10","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB"}};
2、通过构造函数将json字符串转换成json对象
JSONObject jsonObject=new JSONObject(jsonStr);
3、从json对象中获取你所需要的键所对应的值
JSONObject json=jsonObject.getJSONObject("weatherinfo");
String city=json.getString("city");
String temp=json.getString("temp")
字符串in一般是联网取得数据,
解析数组:
解析步骤
1、读取js文件源代码,获取一个json字符串
String in = {"weatherinfo":{"city":"北京","cityid":"101010100","temp":"21","WD":"东风","WS":"级","SD":"78%","WSE":"2","time":"21:10","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB"}};
2、通过构造函数将json字符串转换成json数组
JSONArray array = new JSONArray(jsonStr);
3、遍历数组,获取数组中每一个json对象,同时可以获取json对象中键对应的值
for (int i = 0; i < array.length(); i++) {
JSONObject obj = array.getJSONObject(i);
String title=obj.getString("title");
String description=obj.getString("description");
}
1、创建一个map,通过构造方法将map转换成json对象
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "zhangsan");
map.put("age", 24);
JSONObject json = new JSONObject(map);
2、创建一个json对象,通过put方法添加数据
JSONObject json=new JSONObject();
json.put("name", "zhangsan");
json.put("age", 24);
JSON与xml比较:
1、son比xml文件更容易解析
2、json数据占用空间大小通常要比xml小
3、xml是文件级别,json是数据级别,可以存在于多种文件中
自己练习的代码,就是从地址上获得一个JSON格式,然后通过解析放到自己的界面的显示一些数据:
结果图:
2、JSON的规则很简单:对象是一个无序的“‘名称/值’对”集合.映射的集合(对象)用大括号(“{}”)表示,并列数据的集合(数组)用方括号(“[]”)表示.
3、元素值可具有的类型:string, number, object, array, true, false, null
4、组合的例子:[
{ "name": "张三", "age":22, "email": "zhangsan@qq.com" },
{ "name": "李四", "age":23, "email": "lisi@qq.com"},
{ "name": "王五", "age":24, "email": "wangwu@qq.com" }
]
解析步骤
1、读取html文件源代码,获取一个json字符串
String in = {"weatherinfo":{"city":"北京","cityid":"101010100","temp":"21","WD":"东南风","WS":"2级","SD":"78%","WSE":"2","time":"21:10","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB"}};
2、通过构造函数将json字符串转换成json对象
JSONObject jsonObject=new JSONObject(jsonStr);
3、从json对象中获取你所需要的键所对应的值
JSONObject json=jsonObject.getJSONObject("weatherinfo");
String city=json.getString("city");
String temp=json.getString("temp")
字符串in一般是联网取得数据,
解析数组:
解析步骤
1、读取js文件源代码,获取一个json字符串
String in = {"weatherinfo":{"city":"北京","cityid":"101010100","temp":"21","WD":"东风","WS":"级","SD":"78%","WSE":"2","time":"21:10","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB"}};
2、通过构造函数将json字符串转换成json数组
JSONArray array = new JSONArray(jsonStr);
3、遍历数组,获取数组中每一个json对象,同时可以获取json对象中键对应的值
for (int i = 0; i < array.length(); i++) {
JSONObject obj = array.getJSONObject(i);
String title=obj.getString("title");
String description=obj.getString("description");
}
1、创建一个map,通过构造方法将map转换成json对象
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "zhangsan");
map.put("age", 24);
JSONObject json = new JSONObject(map);
2、创建一个json对象,通过put方法添加数据
JSONObject json=new JSONObject();
json.put("name", "zhangsan");
json.put("age", 24);
JSON与xml比较:
1、son比xml文件更容易解析
2、json数据占用空间大小通常要比xml小
3、xml是文件级别,json是数据级别,可以存在于多种文件中
自己练习的代码,就是从地址上获得一个JSON格式,然后通过解析放到自己的界面的显示一些数据:
public class TestArrayListJsonActivity extends Activity { private ListView mListView; private ProgressBar mProgressBar; String url = "http://192.168.1.203/json/json_x"; List<HashMap<String, String>> mJsonListToMapList; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_json_arraylist_layout); mListView = (ListView) findViewById(R.id.json_list_arraylist); mProgressBar = (ProgressBar) findViewById(R.id.json_before_progressbar); mJsonListToMapList = new ArrayList<HashMap<String, String>>(); // 采用了异步任务去联网取数据,否则肯呢过堵塞主线程 new AsyncTask<String, Void, String>() { protected String doInBackground(String... params) { try { String banckStr = getHttpToJson(params[0]); JSONArray jsonList = new JSONArray(banckStr); for (int i = 0; i < jsonList.length(); i++) { JSONObject jsonObject = jsonList.getJSONObject(i); String title = jsonObject.getString("title"); String description = jsonObject .getString("description"); String image = jsonObject.getString("image"); String comment = jsonObject.getString("comment"); Log.i("11", "地址>>:" + image); HashMap<String, String> map = new HashMap<String, String>(); map.put("title", title); map.put("description", description); map.put("comment", comment + "条评论"); mJsonListToMapList.add(map); } } catch (JSONException e) { e.printStackTrace(); } return null; } }.execute(url); /** * 弄了一个适配器,添加到ListView控件上,注意的是:在适配器里面添加ArrayList的时候可能List数组为空, * 因为主线程和子线程的时间并不能确定 * .如果你用自己封装的网络取数据的类,出现空指针,就把给适配器设置数据源的那段代码放到遍历Json循环外面那。 */ //ProgressBar模拟加载界面,让子线程去网络取数据,取完就隐藏ProgressBar mProgressBar.setVisibility(View.GONE); SimpleAdapter adapter = new SimpleAdapter(this, mJsonListToMapList, R.layout.listview_json_arraylist_layout, new String[] { "title", "description", "comment" }, new int[] { R.id.json_arraylist_title, R.id.json_arraylist_content, R.id.json_arraylist_number }); mListView.setAdapter(adapter); } /** * 进行Get请求网络连接,返回JSON格式的数据源 */ public String getHttpToJson(String abc) { try { HttpClient httpClient = new DefaultHttpClient(); // HttpGet创建get请求方式 HttpGet httpGet = new HttpGet(abc); // 取得HttpResponse,进行get请求 HttpResponse response = httpClient.execute(httpGet); // HttpStatus.SC_OK 封装了过后的返回码,值时200 int n = response.getStatusLine().getStatusCode(); //判断返回码,200才是连接成功的标识 if (n == HttpStatus.SC_OK) { String line = EntityUtils.toString(response.getEntity(), "UTF-8"); return line; } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } }
结果图:
相关文章推荐
- 学习Javascript闭包(Closure)
- 学习Javascript闭包(Closure)
- underscore笔记
- JavaScript语言基础知识7
- 如何配置Spket环境的ExtJS支持
- json日期格式话
- 反混淆JavaScript
- 如何使用Createjs来编写HTML5游戏(八) 通过继承扩展EaselJS中的基础元素
- 保存时,使用JS进行非空判断时,如何获取单选按钮,选定的值?
- 准备用Extjs+ssh做一个小的web系统
- JavaScript eval() Function
- index.js 一种js的写法模式
- js根据时区修改时间
- js 操作jsonarray 创建、添加元素
- 表格内容排序(js实现)
- 02_06 JSP内置对象之session
- JavaScript 开发的44个经典技巧
- 浅谈JS闭包和this关键字
- 学习zepto.js(原型方法)[1]
- Eclipse去除js(JavaScript)验证错误介绍