JSONObject 保存和传送 数组数据 ,如int[] ,double[] ,String[]...

前端之家收集整理的这篇文章主要介绍了JSONObject 保存和传送 数组数据 ,如int[] ,double[] ,String[]...前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

上图为转换后的显示效果。下面介绍如何转换

  1. public class MainActivity extends Activity {
  2.  
  3. private TextView show;
  4.  
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. show = (TextView) findViewById(R.id.show);
  10. jsonSendArray();
  11.  
  12. }
  13. /**
  14. * 用json发送数据 如int [],double[],String [] ....
  15. * 这里是封装并发送 ,方法略显笨拙
  16. */
  17. public void jsonSendArray(){
  18. int [] dou={1,2,3,4,5,6,7,8,9,0};
  19. JSONObject clientKey = new JSONObject();
  20. JSONArray jsonArray = new JSONArray(); //保存数组数据的JSONArray对象
  21. for(int i = 0 ; i < dou.length ;i++){ //依次将数组元素添加进JSONArray对象中
  22. jsonArray.put(dou[i]);
  23. // 两种添加方式一样
  24. // try {
  25. // jsonArray.put(i,dou[i]);
  26. // } catch (JSONException e) {
  27. // // TODO Auto-generated catch block
  28. // e.printStackTrace();
  29. // }
  30. }
  31. try {
  32. clientKey.put("intArray",jsonArray);//jsonArray相当于int数组
  33. show.setText(clientKey.toString()); //输出测试
  34. } catch (JSONException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. }
下面介绍一些JSONObject 和JSONArray的一些基础用法

  1. /**
  2. * 一组数据转换成json
  3. * */
  4. public void string2json(){
  5. String str="{\"name\":\"Iyangc\",\"age\":21,\"sex\":'男',\"address\":{\"province\":\"四川\",\"city\":\"成都\"}}";
  6. try {
  7. JSONObject jsonObject = new JSONObject(str);
  8. String name = jsonObject.getString("name");
  9. JSONObject address = jsonObject.getJSONObject("address");
  10. String province = address.getString("province");
  11. int age = jsonObject.getInt("age");
  12. show.setText("name: "+name+"\nprovince: "+province+"\nage: "+age);
  13. } catch (JSONException e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. /**
  18. * 数组数据转换json
  19. * */
  20. public void json2String(){
  21. String str= "[{\"name\":\"张一\",\"age\":22,\"address\":" +
  22. "{\"province\":\"四川\",\"city\":\"成都\",\"details\":\"光明街16号\"}}," +
  23. "{\"name\":\"张二\",\"age\":23,\"details\":\"建设路5号\"}}," +
  24. "{\"name\":\"张三\",\"details\":\"中山南路8号\"}}]";
  25. try {
  26. JSONArray jsonArray = new JSONArray(str); //将String转换成JsonArray对象
  27. int length=jsonArray.length();
  28. //取出数据
  29. for (int i = 0; i < length; i++) {
  30. JSONObject jsonObject = jsonArray.getJSONObject(i);
  31. String name = jsonObject.getString("name");
  32. JSONObject jsonObject2 = jsonObject.getJSONObject("address");
  33. String province = jsonObject2.getString("province");
  34. System.out.println(name+"-----------"+province);
  35. }
  36. } catch (JSONException e) {
  37. // TODO Auto-generated catch block
  38. e.printStackTrace();
  39. }
  40. }
  41. /**
  42. * 以键值对写入json
  43. * */
  44. public static void jsonArray2String(){
  45. JSONArray jsonArray = new JSONArray();
  46. JSONObject jsonObject1= new JSONObject();
  47. JSONObject jsonObject2= new JSONObject();
  48. JSONObject jsonObject3= new JSONObject();
  49. try {
  50. jsonObject1 .put("name","Iyangc");
  51. jsonObject1 .put("age",21);
  52. JSONObject addressObject1 = new JSONObject();
  53. addressObject1.put("province","四川");
  54. jsonObject1.put("address",addressObject1 );
  55. jsonObject2 .put("name","ILan");
  56. jsonObject2 .put("age",22);
  57. JSONObject addressObject2 = new JSONObject();
  58. addressObject2.put("province","四川简阳");
  59. jsonObject2.put("address",addressObject2 );
  60. jsonObject3 .put("name","IBo");
  61. jsonObject3 .put("age",21);
  62. JSONObject addressObject3 = new JSONObject();
  63. addressObject3.put("province","四川石桥");
  64. jsonObject3.put("address",addressObject3 );
  65. jsonArray.put(jsonObject1);
  66. jsonArray.put(jsonObject2);
  67. jsonArray.put(jsonObject3);
  68. //打印输出转换成json格式后的String字符串
  69. String string = jsonArray.toString();
  70. System.out.println(string );
  71. } catch (JSONException e) {
  72. // TODO Auto-generated catch block
  73. e.printStackTrace();
  74. }
  75. }
就单独用JSON的话 解析起来会显得很麻烦 所以一般要和Gson一起使用。 Gson可以直接将Json类型的字符串转换成类对象,并且对象的属性值与Json的属性值相对应。Gson的一个免费下载地址:http://download.csdn.net/detail/a771948524/6668573

下载后 将其jar包导入工程就可以使用了。下面是Gson的一些基本用法

  1. public class GsonDemo extends Activity {
  2. Ure ure1;
  3. Ure ure2;
  4. Ure ure3;
  5. ArrayList<Ure> ures;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.gson_demo);
  10. ures=new ArrayList<Ure>();
  11. ure1=new Ure("Iyangc",21);
  12. ure2=new Ure("Iyang",22);
  13. ure3=new Ure("Iyan",23);
  14. ures.add(ure1);
  15. ures.add(ure2);
  16. ures.add(ure3);
  17. // strToJson();
  18. // listToJson();
  19. // strFromJson();
  20. listFromJson();
  21. }
  22. /**
  23. * 通过Gson将bean转化成JSON数据
  24. * */
  25. public void strToJson(){
  26. Gson g=new Gson();
  27. //通过Gson.toJson将bean转成Json对象
  28. String json = g.toJson(ure1);
  29. System.out.println(json );
  30. }
  31. /**
  32. * 通过Gson.toJson将bean集合转成Json对象
  33. * */
  34. public void listToJson(){
  35. Gson g=new Gson();
  36. //通过Gson.toJson将bean集合转成Json对象
  37. String json = g.toJson(ures);
  38. System.out.println(json );
  39. }
  40. /**
  41. * 将JSON格式的数据解析出来~~
  42. * */
  43. public void strFromJson(){
  44. Ure ure=new Ure();
  45. Gson g=new Gson();
  46. String str="{\"name\":\"ILan\",\"age\":22}";
  47. // JSONObject json=new JSONObject(str);
  48. ure=g.fromJson(str,Ure.class);
  49. System.out.println(ure.name+" ======= "+ure.age);
  50. }
  51. /**
  52. * 将JSON格式的数据(集合)解析出来~~
  53. * */
  54. public void listFromJson(){
  55. Gson g=new Gson();
  56. String str="[{\"name\":\"ILan1\",\"age\":221},{\"name\":\"ILan2\",\"age\":222}]";
  57. // JSONObject json=new JSONObject(str);
  58. Type type = new TypeToken<List<Ure>>(){ }.getType();
  59. //将JSON集合数据解析出来
  60. List<Ure> l=g.fromJson(str,type);
  61. for (Ure ure : l) {
  62. System.out.println(ure.name+" ======= "+ure.age);
  63. }
  64. }
  65. }
  66. class Ure{
  67. public String name;
  68. public int age;
  69. public Ure(String name,int age) {
  70. this.name = name;
  71. this.age = age;
  72. }
  73. public Ure() {
  74. }
  75. }

在JSONObject 传送数组时 可以直接在bean类中定义一个数组 ,只要属性名对应且类型对应 用Gson可直接转换出来,就和基本类型一样。
如果 Ure类中多一个 double[] 数组对象dou,而且json中有对应数据 那么用Gson将json转换成use对象后 直接 use.dou就能获取这个数组数据。

Demo下载地址: http://download.csdn.net/detail/yangbo437993234/7626045 不懂的可以下来看看 都是很基础的

猜你在找的Json相关文章