json的初步学习
来源:互联网 发布:怎么评价曼德拉 知乎 编辑:程序博客网 时间:2024/05/11 14:05
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格式,然后通过解析放到自己的界面的显示一些数据:
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模拟加载界面,让子线程去网络取数据,取完就隐藏ProgressBarmProgressBar.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 封装了过后的返回码,值时200int 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;}}
结果图:
- json的初步学习
- json的初步学习
- json的初步学习
- Json的初步学习、接触
- JSON的初步应用
- json的初步认识
- json的初步理解
- JSON的初步理解
- [JAVA][JSON]初步学习JSON<一>
- 对JSON的初步认识
- JSON初步
- 编程的初步学习
- Smarty的初步学习
- OpenMP的学习初步
- 模板的初步学习
- bash的初步学习
- AsyncTask的初步学习
- startActivityForResult的初步学习
- 线性时间查找中位数算法
- hdoj 1728 逃离迷宫 [bfs]
- swift-错误unrecognized selector sent to instance
- iOS网络编程实践--NSStream实现TCP Socket iPhone客户端
- shelll编程-项目笔记
- json的初步学习
- 剑指Offer面试题40(Java版):数组出现一次的数字
- Matlab绘图函数
- Eclipse工程项目前有红色的感叹号
- java的一些基础
- Laravel 5.1.4 + Bootstrap 3.3.5 笔记三:Laravel 中间件
- IOS 网络编程 + 后台保持连接
- MySQL索引建立、优点、不足
- 多线程的调度,以及进程的调度