下面介绍一些JSONObject 和JSONArray的一些基础用法
- public class MainActivity extends Activity {
- private TextView show;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- show = (TextView) findViewById(R.id.show);
- jsonSendArray();
- }
- /**
- * 用json发送数据 如int [],double[],String [] ....
- * 这里是封装并发送 ,方法略显笨拙
- */
- public void jsonSendArray(){
- int [] dou={1,2,3,4,5,6,7,8,9,0};
- JSONObject clientKey = new JSONObject();
- JSONArray jsonArray = new JSONArray(); //保存数组数据的JSONArray对象
- for(int i = 0 ; i < dou.length ;i++){ //依次将数组元素添加进JSONArray对象中
- jsonArray.put(dou[i]);
- // 两种添加方式一样
- // try {
- // jsonArray.put(i,dou[i]);
- // } catch (JSONException e) {
- // // TODO Auto-generated catch block
- // e.printStackTrace();
- // }
- }
- try {
- clientKey.put("intArray",jsonArray);//jsonArray相当于int数组
- show.setText(clientKey.toString()); //输出测试
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }
就单独用JSON的话 解析起来会显得很麻烦 所以一般要和Gson一起使用。 Gson可以直接将Json类型的字符串转换成类对象,并且对象的属性值与Json的属性值相对应。Gson的一个免费下载地址:http://download.csdn.net/detail/a771948524/6668573
- /**
- * 一组数据转换成json
- * */
- public void string2json(){
- String str="{\"name\":\"Iyangc\",\"age\":21,\"sex\":'男',\"address\":{\"province\":\"四川\",\"city\":\"成都\"}}";
- try {
- JSONObject jsonObject = new JSONObject(str);
- String name = jsonObject.getString("name");
- JSONObject address = jsonObject.getJSONObject("address");
- String province = address.getString("province");
- int age = jsonObject.getInt("age");
- show.setText("name: "+name+"\nprovince: "+province+"\nage: "+age);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- /**
- * 数组数据转换json
- * */
- public void json2String(){
- String str= "[{\"name\":\"张一\",\"age\":22,\"address\":" +
- "{\"province\":\"四川\",\"city\":\"成都\",\"details\":\"光明街16号\"}}," +
- "{\"name\":\"张二\",\"age\":23,\"details\":\"建设路5号\"}}," +
- "{\"name\":\"张三\",\"details\":\"中山南路8号\"}}]";
- try {
- JSONArray jsonArray = new JSONArray(str); //将String转换成JsonArray对象
- int length=jsonArray.length();
- //取出数据
- for (int i = 0; i < length; i++) {
- JSONObject jsonObject = jsonArray.getJSONObject(i);
- String name = jsonObject.getString("name");
- JSONObject jsonObject2 = jsonObject.getJSONObject("address");
- String province = jsonObject2.getString("province");
- System.out.println(name+"-----------"+province);
- }
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**
- * 以键值对写入json
- * */
- public static void jsonArray2String(){
- JSONArray jsonArray = new JSONArray();
- JSONObject jsonObject1= new JSONObject();
- JSONObject jsonObject2= new JSONObject();
- JSONObject jsonObject3= new JSONObject();
- try {
- jsonObject1 .put("name","Iyangc");
- jsonObject1 .put("age",21);
- JSONObject addressObject1 = new JSONObject();
- addressObject1.put("province","四川");
- jsonObject1.put("address",addressObject1 );
- jsonObject2 .put("name","ILan");
- jsonObject2 .put("age",22);
- JSONObject addressObject2 = new JSONObject();
- addressObject2.put("province","四川简阳");
- jsonObject2.put("address",addressObject2 );
- jsonObject3 .put("name","IBo");
- jsonObject3 .put("age",21);
- JSONObject addressObject3 = new JSONObject();
- addressObject3.put("province","四川石桥");
- jsonObject3.put("address",addressObject3 );
- jsonArray.put(jsonObject1);
- jsonArray.put(jsonObject2);
- jsonArray.put(jsonObject3);
- //打印输出转换成json格式后的String字符串
- String string = jsonArray.toString();
- System.out.println(string );
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
下载后 将其jar包导入工程就可以使用了。下面是Gson的一些基本用法:
- public class GsonDemo extends Activity {
- Ure ure1;
- Ure ure2;
- Ure ure3;
- ArrayList<Ure> ures;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.gson_demo);
- ures=new ArrayList<Ure>();
- ure1=new Ure("Iyangc",21);
- ure2=new Ure("Iyang",22);
- ure3=new Ure("Iyan",23);
- ures.add(ure1);
- ures.add(ure2);
- ures.add(ure3);
- // strToJson();
- // listToJson();
- // strFromJson();
- listFromJson();
- }
- /**
- * 通过Gson将bean转化成JSON数据
- * */
- public void strToJson(){
- Gson g=new Gson();
- //通过Gson.toJson将bean转成Json对象
- String json = g.toJson(ure1);
- System.out.println(json );
- }
- /**
- * 通过Gson.toJson将bean集合转成Json对象
- * */
- public void listToJson(){
- Gson g=new Gson();
- //通过Gson.toJson将bean集合转成Json对象
- String json = g.toJson(ures);
- System.out.println(json );
- }
- /**
- * 将JSON格式的数据解析出来~~
- * */
- public void strFromJson(){
- Ure ure=new Ure();
- Gson g=new Gson();
- String str="{\"name\":\"ILan\",\"age\":22}";
- // JSONObject json=new JSONObject(str);
- ure=g.fromJson(str,Ure.class);
- System.out.println(ure.name+" ======= "+ure.age);
- }
- /**
- * 将JSON格式的数据(集合)解析出来~~
- * */
- public void listFromJson(){
- Gson g=new Gson();
- String str="[{\"name\":\"ILan1\",\"age\":221},{\"name\":\"ILan2\",\"age\":222}]";
- // JSONObject json=new JSONObject(str);
- Type type = new TypeToken<List<Ure>>(){ }.getType();
- //将JSON集合数据解析出来
- List<Ure> l=g.fromJson(str,type);
- for (Ure ure : l) {
- System.out.println(ure.name+" ======= "+ure.age);
- }
- }
- }
- class Ure{
- public String name;
- public int age;
- public Ure(String name,int age) {
- this.name = name;
- this.age = age;
- }
- public Ure() {
- }
- }
在JSONObject 传送数组时 可以直接在bean类中定义一个数组 ,只要属性名对应且类型对应 用Gson可直接转换出来,就和基本类型一样。
如果 Ure类中多一个 double[] 数组对象dou,而且json中有对应数据 那么用Gson将json转换成use对象后 直接 use.dou就能获取这个数组数据。
Demo下载地址: http://download.csdn.net/detail/yangbo437993234/7626045 不懂的可以下来看看 都是很基础的