json数据的解析和把数据转换成json格式的字符串
来源:互联网 发布:域名要备案才能解析吗 编辑:程序博客网 时间:2024/05/27 09:44
json有四种行式1 简单的一个 { ,,,,}
2 里面有数组 { , [{,,},{,,}],}
3 直接一个数组
4 数组里有数组
package com.example.jsontest;import java.util.ArrayList;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import android.support.v7.app.ActionBarActivity;import android.util.Log;import android.os.Bundle;public class MainActivity extends ActionBarActivity {private ArrayList<Ping> data = new ArrayList<Ping>();private ArrayList<Ping> data1 = new ArrayList<Ping>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);addData();JSONObject json = new JSONObject();JSONArray ja = new JSONArray();JSONArray ja1 = new JSONArray();for( Ping a : data){ //把list里的值转OBJECTJSONObject obj = new JSONObject();try {obj.put("id", a.getA()); obj.put("name", a.getName());} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}ja.put(obj);//把object加入到JSONArray }for( Ping a : data1){JSONObject obj = new JSONObject();try {obj.put("id", a.getA());obj.put("name", a.getName());} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}ja1.put(obj);}try {json.put("data", ja); //把JSONArray加入JSONObjectjson.put("data1", ja1);} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}//Log.e("", json.toString());json(json.toString());}private void json(String a){try {JSONObject obj = new JSONObject(a);JSONArray jsonArray = obj.getJSONArray("data");JSONArray jsonArray1 = obj.getJSONArray("data1");for (int i = 0; i <jsonArray.length(); i++) {JSONObject jsonObj = jsonArray.getJSONObject(i);String name =jsonObj.getString("name");String id = jsonObj.getString("id");Log.e("", name +"--"+id);}for (int i = 0; i <jsonArray1.length(); i++) {JSONObject jsonObj = jsonArray1.getJSONObject(i);String name1 =jsonObj.getString("name");String id1 = jsonObj.getString("id");Log.e("", name1 +"--"+id1);}} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}}private void addData() {for (int i = 0; i < 10; i++) {Ping item = new Ping();item.setA(i);item.setName("小明"+i);data.add(item);}for (int i = 0; i < 10; i++) {Ping item = new Ping();item.setA(i);item.setName("小红"+i);data1.add(item);}}}package com.example.jsontest;public class Ping {private String name;private int a;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getA() {return a;}public void setA(int a) {this.a = a;}}
0 0
- android实现json数据的解析和把数据转换成json格式的字符串
- android实现json数据的解析和把数据转换成json格式的字符串
- android实现json数据的解析和把数据转换成json格式的字符串
- android实现json数据的解析和把数据转换成json格式的字符串
- android实现json数据的解析和把数据转换成json格式的字符串
- json数据的解析和把数据转换成json格式的字符串
- 把数据转换成json格式的字符串
- 把dataset数据转换成json的格式通用方法
- 把dataset数据转换成json的格式通用方法
- 把user对象里面的数据解析成json格式
- 将map型数据转换成json格式的字符串
- 把数据转换成json格式
- android 将数据转换成JSON数据格式并使用JSONObject解析JSON格式的数据
- 解析json格式的数据
- JSON数据的转换和解析方法
- Json解析 开发实践 XML和JSON格式的数据
- NSDictionary转换成json格式的数据
- 数组转换成json格式的数据
- 查看Oracle 数据库中的长事务
- win10输入法切换 只能输入英文
- XML基础(下)
- Ceph架构
- 20.IDA-修改二进制文件、显示修改点
- json数据的解析和把数据转换成json格式的字符串
- HDU 5624 KK's Reconstruction(暴力 + MST)
- 9.1、神经网络实例
- Photoshop CS6 实例之用色彩范围抠图并合成背景
- UVa1339
- [BZOJ2054] 疯狂的馒头/[BZOJ2375] 疯狂的涂色
- Presenting view controllers on detached view controllers is discouraged的解决方案
- [剑指offer-1516]调整数组顺序使奇数位于偶数前面
- 数据结构--顺序表