json在线解析器篇一
《JSON解析》
1:JSON解析:在Java中没有相关的类所以他是Android自有的。
2:与DOM和SAX解析的区别:他也是调用API进行解析,不同的是他的解析的方式不同,文档的格式也是不同的。
3:JSON是什么:(JavaScript Object Notation)意为javaScript对象表示法,是一种基于文本,独立语言的轻量级数据交换格式。
优势:易于人阅读和便携,同时也易于机器解析和生成
4:JSON基础结构:
结构类型:对象(JsONObject)与数组(JSONArray)
对象:是一个无序的““名称/值” 对”集合
对象结构以“{”大括号开始,以“}”大括号结束
每个“名称”后跟一个“:”冒号
“名称/值”对之间用“,”(逗号)隔开
语法结构如下
{
Key1:value1,
Key2:value2,
……..
}
例如:{“name”:“张三”,“age”:24}注意:名称是字符串,值是字符串或者数字或者对象或者数组
数组:
值(value)是一个有序的集合
一个数组以“[”开始“]”结束
值之间用“,”分隔
语法结构如下
[
{
Key1:value1,
Key2:value2
}
]值可以是字符串,数字,对象或者数组
例子:
{“student”:[{“name”: “zhangsan”,”age” :11},{“name”:”lisi”, “age”:24 }]}
5:
public class MainActivity extends Activity {
//从网络发送过来一串数据:
//{"name":"李天保","age":25,"sex":"male"}
String JSON="{\"name\":\"李天保\",\"age\":25,\"sex\":\"male\"}";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
package com.example.json02;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
//从网络传过来一些数据通过Json解析对数据进行提取,删除,修改,添加等操作 String jsonData="{\"name\":\"李天保\",\"age\":\"25\",\"sex\":\"man\"}";//网络获取的数据
String jsonData01="{\"name\":\"李天保\",\"age\":\"25\",\"sex\":\"man\"}"; String jsonObjectArry="{\"student\":[{\"name\":\"李天保
\",\"age\":\"25\",\"sex\":\"man\"},{\"name\":\"何倩
\",\"age\":\"26\",\"sex\":\"wman\"}]}";
String JsonArray="[\"李天保\",\"age\",\"25\",\"sex\",\"man\"]";
protected void onCreate(Bundle savedInstanceState) {json在线解析器。
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
System.out.println("222222222");
JSONObject jsonObject=new JSONObject(jsonObjectArry);//Jsonobject对象
System.out.println("111111111");
try {
//addData(jsonObject);
jsonObjectArray(jsonObject);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (JSONException e) {
e.printStackTrace();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// try {
//json在线解析器。
// JSONArray jsonArray=new JSONArray(JsonArray);
// System.out.println("000000000000000000"+jsonArray);json在线解析器。
// try {
// jsonArray(jsonArray);
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// } catch (JSONException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
/**
* 查询数据
*
*/
public void demandData(JSONObject jsonObject) throws Exception{
int jsonAge=jsonObject.getInt("age");
String jsonSex=jsonObject.getString("sex");
String jsonName=jsonObject.getString("name");//获取数据中的姓名的值 System.out.println("查询数据Age:"+jsonAge);
System.out.println("查询数据name:"+jsonName);
System.out.println("查询数据sex:"+jsonSex);
}
/**
* 添加数据
*/
public void addData(JSONObject jsonObject) throws Exception{
jsonObject.put("phoneNum", 1820918346);
jsonObject.put("address", "内蒙古赤峰市敖汉旗");
//添加数据可以是普通的int也可以是字符串等数据,当然也可以是对象 jsonObject.put("student",jsonData01);
System.out.println("JsonData添加完数据之后的数据:
"+jsonObject.toString());
}
/**
* 利用Json数组进行处理
* @throws Exception
*/
public void jsonArray(JSONArray jsonArray) throws Exception{
//获取数据
String Jsonobject01=(String) jsonArray.get(0);
//添加数据
jsonArray.put(4, "李春来");//修改数据
System.out.println(jsonArray.toString());
}
/**
* 对象中查灿掺杂数组
* @throws Exception
*/
public void jsonObjectArray(JSONObject jsonObject) throws Exception{ /**
* 一步一步去获得对象中的数组在获得数组中的对象在获得对象的具体内容 */
String jsonArrayDate=jsonObject.get("student").toString();//获取数组的字符串
JSONArray array=new JSONArray(jsonArrayDate);//创建Josn数组并加载数组的字符串
String jsonobejiectdata01=array.get(0).toString();//获取数组中的第一个位置的内容
JSONObject jsonObject2=new JSONObject(jsonobejiectdata01);//创建数组中的JsonObeject对象并将数组第一个位置的对象加载
String name=jsonObject2.get("name").toString();//获取对象之后继续查找对象中键值的
System.out.println("99999999999999999999999999999"+name);
/**
* 调用优良方法不用那么麻烦
*/
JSONArray jsonArray=jsonObject.getJSONArray("student");//直接获得数组不用加载对象
System.out.println("jsonarray+++++++++++++++++"+jsonArray.toString());
}
}
JSONObject jsonObject=new JSONObject(JSON);
String name=jsonObject.getString("name");
int age=jsonObject.getInt("age");
String sex=jsonObject.getString("sex");
//System.out.println("姓名:"+name+"年龄:"+age+"性别:"+sex); jsonObject.put("电话", 110);
System.out.println(jsonObject.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
} } e.printStackTrace(); }
https://m.shanpow.com/bg/147/
推荐访问:json在线解析器高亮 json在线解析器下载