数据储存——远程服务器存储——JSON

摘要:
paddingBottom=“@dimn/activity_vertical_margin”7 android:ppaddingLeft=“@Dimn/activity_horizontal_margin)8 android:pridingTop=“@DIMn/activity_vertical_markin”10工具:orientation=“vertical”&gt:

JSON

一.特点

    1.JavaScript Object Notation

    2.一种轻量级的数据交互格式

二.格式

    1.[ ] 数组:[value1, value2, value3...]

    2.{ } 对象:{key1:value1, key2:value2, key3:value3,...}

      1-key:字符串,表示对象的属性

      2-value:表示属性的值

         数据类型:数值,字符串,null,json数组,json对象。

三.API

   1.Android原生

   2.第三方框架

数据储存——远程服务器存储——JSON第1张数据储存——远程服务器存储——JSON第2张
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.hanqi.testapp3.TestActivity4"
11     android:orientation="vertical">
12 
13     <Button
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:text="Android原生解析JSON转对象"
17         android:onClick="bt1_OnClick"/>
18 
19     <Button
20         android:layout_width="match_parent"
21         android:layout_height="wrap_content"
22         android:text="Gson解析JSON转对象"
23         android:onClick="bt2_OnClick"/>
24 
25     <Button
26         android:layout_width="match_parent"
27         android:layout_height="wrap_content"
28         android:text="Android原生解析JSON转集合"
29         android:onClick="bt3_OnClick"/>
30 
31     <Button
32         android:layout_width="match_parent"
33         android:layout_height="wrap_content"
34         android:text="Gson解析JSON转集合"
35         android:onClick="bt4_OnClick"/>
36 
37     <Button
38         android:layout_width="match_parent"
39         android:layout_height="wrap_content"
40         android:text="Gson解析JSON之对象转JSON"
41         android:onClick="bt5_OnClick"/>
42 
43     <Button
44         android:layout_width="match_parent"
45         android:layout_height="wrap_content"
46         android:text="Gson解析JSON之集合转JSON"
47         android:onClick="bt6_OnClick"/>
48 
49 
50 
51 
52     <EditText
53         android:layout_width="match_parent"
54         android:layout_height="200dp"
55         android: />
56 
57 
58 </LinearLayout>
59 
60 .xml
.xml
数据储存——远程服务器存储——JSON第3张数据储存——远程服务器存储——JSON第4张
  1 package com.hanqi.testapp3;
  2 
  3 import android.support.v7.app.AppCompatActivity;
  4 import android.os.Bundle;
  5 import android.view.View;
  6 import android.widget.EditText;
  7 
  8 import com.google.gson.Gson;
  9 import com.google.gson.reflect.TypeToken;
 10 
 11 import org.json.JSONArray;
 12 import org.json.JSONException;
 13 import org.json.JSONObject;
 14 
 15 import java.util.ArrayList;
 16 import java.util.List;
 17 import java.util.Map;
 18 
 19 public class TestActivity4 extends AppCompatActivity {
 20 
 21     EditText et_3;
 22 
 23     @Override
 24     protected void onCreate(Bundle savedInstanceState) {
 25         super.onCreate(savedInstanceState);
 26         setContentView(R.layout.activity_test4);
 27 
 28         et_3=(EditText)findViewById(R.id.et_3);
 29 
 30     }
 31 
 32     //原生从Json字符串转成对象
 33     public void bt1_OnClick(View v)
 34     {
 35 
 36         String strJson="{"id":1,"name":"大虾", " +
 37                 ""price":12.3, " +
 38                 ""imagePath":"http://192.168.10.165:8080/L05_Server/images/f1.jpg"}";
 39 
 40         //从Json字符串转成对象
 41         try {
 42             JSONObject jo = new JSONObject(strJson);
 43 
 44             int id=jo.getInt("id");
 45             String name=jo.getString("name");
 46             double price=jo.getDouble("price");
 47             String imagePath=jo.getString("imagePath");
 48 
 49             ShopInfo shopInfo=new ShopInfo(id,imagePath,name,price);
 50 
 51             et_3.setText(shopInfo.toString());
 52 
 53 
 54         }
 55         catch (Exception e)
 56         {
 57             e.printStackTrace();
 58         }
 59 
 60 
 61     }
 62 
 63     //Gson转对象
 64     public void bt2_OnClick(View v)
 65     {
 66         //转对象
 67         String jsonstr="{"id":3,"name":"大虾"," +
 68                 ""price":12.3," +
 69                 ""imagePath":"http://192.168.10.165:8080/L05_Server/images/f1.jpg"}";
 70 
 71         ShopInfo shopInfo=new Gson().fromJson(jsonstr,ShopInfo.class);
 72 
 73         et_3.setText(shopInfo.toString());
 74 
 75 //        //转集合
 76 //        String jsonstr1="[{"id":3, "name":"大虾1", "price":12.3, " +
 77 //                ""imagePath":"http://192.168.10.165:8080/f1.jpg"}," +
 78 //                "{"id":4, "name":"大虾2", "price":12.5, " +
 79 //                ""imagePath":"http://192.168.10.165:8080/f2.jpg"}]";
 80 //
 81 //        List<ShopInfo> list=new Gson().fromJson(jsonstr1, new TypeToken<List<ShopInfo>>() {
 82 //        }.getType());
 83 //
 84 //        et_3.setText(list.toString());
 85 //
 86 //        //对象转JSON
 87 //        ShopInfo info=new ShopInfo(3,"http://www.sina.com","KK",1000);
 88 //
 89 //        String json=new Gson().toJson(info);
 90 //
 91 //        et_3.setText(json);
 92 //
 93 //        //集合转JSON
 94 //        List<ShopInfo> list1=new ArrayList<ShopInfo>();
 95 //
 96 //        list1.add(new ShopInfo(3, "http://www.sina.com","KK", 1000));
 97 //        list1.add(new ShopInfo(4, "http://www.sina.com/cn", "KK1", 2000));
 98 //        String json1=new Gson().toJson(list1);
 99 //
100 //        et_3.setText(json1);
101 
102 //        String jsonstr2="{"my name":"大虾","1":12}";
103 //
104 //        Map<String,Object> map=new Gson().fromJson(jsonstr2,new TypeToken<Map<String,Object>>(){}.getType());
105 //
106 //        et_3.setText(map.toString());
107 
108     }
109 
110     //原生从Json字符串转成集合
111     public void bt3_OnClick(View v)
112     {
113 
114         //转集合
115         String jsonstr="[{"id":1,"name":"大虾1","price":12.3," +
116                 " "imagePath":"http://192.168.10.165:8080/f1.jpg"}," +
117                 "{"id":2, "name":"大虾2", "price":12.5, " +
118                 ""imagePath":"http://192.168.10.165:8080/f2.jpg"}]";
119 
120         //从Json字符串转成集合
121         try {
122 
123             List<ShopInfo> list=new ArrayList<ShopInfo>();
124 
125             //1.将json字符串包装JSONArray对象
126             JSONArray jsonArray=new JSONArray(jsonstr);
127 
128             //2.遍历JSONArray对象所有元素(JSONObject),并将每个元素封装为shopInfo,并添加到list
129             for (int i=0;i<jsonArray.length();i++)
130             {
131                 JSONObject jsonObject=jsonArray.getJSONObject(i);
132 
133                 //从对象中根据key得到相应的value
134                 int id=jsonObject.getInt("id");
135                 String name=jsonObject.getString("name");
136                 double price=jsonObject.getDouble("price");
137                 String imagePath=jsonObject.getString("imagePath");
138 
139                 //封装ShopInfo对象
140                 ShopInfo shopInfo=new ShopInfo(id,imagePath,name,price);
141 
142                 list.add(shopInfo);
143 
144             }
145 
146             et_3.setText(list.toString());
147 
148 
149         }
150         catch (Exception e)
151         {
152             e.printStackTrace();
153         }
154 
155 
156     }
157 
158 
159     //Gson转集合
160     public void bt4_OnClick(View v)
161     {
162         //转集合
163         String jsonstr="[{"id":3, "name":"大虾1", "price":12.3, " +
164                 ""imagePath":"http://192.168.10.165:8080/f1.jpg"}," +
165                 "{"id":4, "name":"大虾2", "price":12.5, " +
166                 ""imagePath":"http://192.168.10.165:8080/f2.jpg"}]";
167 
168         List<ShopInfo> list=new Gson().fromJson(jsonstr, new TypeToken<List<ShopInfo>>() {
169         }.getType());
170 
171         et_3.setText(list.toString());
172 
173 
174     }
175 
176 
177     //Gson对象转JSON
178     public void bt5_OnClick(View v)
179     {
180         //对象转JSON
181         ShopInfo info=new ShopInfo(3,"http://www.sina.com","KK",1000);
182 
183         String json=new Gson().toJson(info);
184 
185         et_3.setText(json);
186 
187 
188     }
189 
190     //Gson集合转JSON
191     public void bt6_OnClick(View v)
192     {
193         //集合转JSON
194         List<ShopInfo> list=new ArrayList<ShopInfo>();
195 
196         list.add(new ShopInfo(3, "http://www.sina.com","KK", 1000));
197         list.add(new ShopInfo(4, "http://www.sina.com/cn", "KK1", 2000));
198         String json=new Gson().toJson(list);
199 
200         et_3.setText(json);
201 
202 
203     }
204 
205 
206 }
207 
208 .java
.java

数据储存——远程服务器存储——JSON第5张

数据储存——远程服务器存储——JSON第6张

数据储存——远程服务器存储——JSON第7张

数据储存——远程服务器存储——JSON第8张

数据储存——远程服务器存储——JSON第9张

数据储存——远程服务器存储——JSON第10张

数据储存——远程服务器存储——JSON第11张

免责声明:文章转载自《数据储存——远程服务器存储——JSON》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇如何把C盘里的文件默认位置更改到D盘指定目录?Window.requestAnimationFrame()动画更新下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

阿里人脸识别接口

最近研究了一下阿里的人脸识别功能,只是调用阿里提供的接口返回数据给我们就OK了。详细文档可参考阿里的api  阿里人脸识别api 人脸比对的方法 直接上代码吧: 1 package com.guoxin.common.test; 2 3 import sun.misc.BASE64Encoder; 4 import javax.cryp...

vue $forceUpdate()强制刷新

改变列表的值 一直不渲染 <van-pull-refresh v-model="refreshing" @refresh="onRefresh"> <van-list v-model="loading" :finish...

微信小程序实现国旗头像,国庆个性化头像。国庆头像

如需自取,完整项目源码:https://gitee.com/vxsoft/online-h 若对你有帮助,烦请star一个 请给我一面国旗@微信官方,先上生成的头像效果图  小程序的制作国庆头像的页面 利用 canvas 绘制头像: 核心代码: wx.canvasToTempFilePath({ x: 0,...

c# 构造tree下拉框,空格转化

c#代码写的空格如何在html中的select中展示出来呢? var str = ""; //父级菜单不缩进 for (var j = 1; j < i; j++) { s...

MybatisPlus学习笔记7:插件的配置

MP提供了很多好用的插件,而且配置简单,使用方便。接下来一起看看MP的插件如何使用。 1、分页插件: 之前就有说到,BaseMapper的selectPage方法和AR提供的selectPage方法都不是物理分页,需要配置分页插件后才是物理分页,那么现在就来看看如何配置这个插件。 在sqlSessionFactory这个bean中,通过配置插件,接下来的所...

Unity调用安卓Android的Toast

需求:在游戏中弹窗消息,调起安卓的Toast 项目中需要做Unity和安卓交互时,经常需要通过安卓Toast来做简单的输出,以便于测试。 方法一:Unity中,C#主导 //Unity调用安卓的土司 public static void MakeToast(stringinfo) { AndroidJavaClass unityPla...