远程服务器存储之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 <?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:id="@+id/et_3"/> 56 57 58 </LinearLayout>
.xml
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 }
.java
转载于:https://www.cnblogs.com/arxk/p/5588916.html
远程服务器存储之JSON相关推荐
- 远程服务器存储之JDK方式
一.API 1.new URL("http://网络资源路径") 2.openConnection(),返回URLConnection对象 二.HttpURLConnection ...
- 服务器 远程存储,数据储存——远程服务器存储——框架方式
一.Volley 1.特点 ①轻量级的Android网络通信库 ②适合数量不大但通信频繁的场景 2.API 1.RequestQueue ①请求队列 ②Volley.newRequestQueue(c ...
- nfs服务器远程访问,NFS远程共享存储
原标题:NFS远程共享存储 构建储NFS远程共享存 因为NFS有很多功能,不同的功能需要使用不同的端口.因此NFS无法固定端口.而RPC会记录NFS端口的信息,这样就能够通过RPC实现服务端和客户端的 ...
- git服务器文件存储结构,在远程服务器搭建gitlab,并将数据单独存储到磁盘
虽然github目前是全球最火的分布式代码托管平台,但是github有一个缺点就是,project如果想要建为private,那就得付费.还有data存储在github平台,而不是自己的本地服务器.相 ...
- post json 提示远程服务器500_解决WinServer2012R2服务器远程提示“参数错误”
最近,小编在远程服务器进行日常的运维的时候,发现服务器无法远程了,提示如下: "参数错误",看到这个提示,小编一脸懵逼,记忆中好像没有对服务器做什么调整,只是前两天对它进行了修改管 ...
- 对应win10的服务器系统,我的系统变成win10远程服务器系统
我的系统变成win10远程服务器系统 内容精选 换一换 等保建设助手为用户提供等保定级和差距评估咨询,根据系统情况提供定级参考意见和相关技术建议书以及等保条款分析情况汇总,根据等保差距要求,服务类型以 ...
- 服务器本地视频怎么网站播放视频教程,远程服务器的视频怎么在本地播放
远程服务器的视频怎么在本地播放 内容精选 换一换 网站的访问与云服务器的网络配置.端口通信.防火墙配置.安全组配置等多个环节相关联.任意一个环节出现问题,都会导致网站无法访问.本节操作介绍网站无法访问 ...
- 怎样访问远程服务器文件夹,访问远程服务器的共享文件夹
访问远程服务器的共享文件夹 内容精选 换一换 FOTA升级作用:在官方更新新固件后,模组设备无需寄回给官方,而是通过远程FTP/HTTP进行OTA升级固件,以更新新固件版本,从而达到更新设备的功能/B ...
- 百度ueditor富文本--图片保存路径的配置以及上传到远程服务器
我们在上篇文章中学习了 上传图片的配置: 百度ueditor富文本--配置图片上传 在文章的最后 讲到 ueditor 默认设置的 保存图片的 路径 是相对路径,项目相关的. 保存的图片会放在to ...
最新文章
- 看看人家 SpringBoot 的全局异常处理,多么优雅...
- 位操作-按位与之如何求二进制数的1个数
- java lock 效率_工作常用4种Java线程锁的特点,性能比较、使用场景
- 计算机格式化后数据恢复的基础,用DiskGenius恢复误删除或误格式化后的文件
- win32api.sendmessage模拟鼠标点击_安卓模拟器一键宏设置教程
- 自定义分页模板(银角大王版)
- 『ACM-算法-图论』算法竞赛进阶指南--hamilton路径(模板)
- shell字符串的用法
- ansible 批量部署ssh免密钥
- oracle宣传视频下载,1300首 Audiomachine 背景音乐电影宣传预告片配乐合辑(23集)...
- 目标检测——Anchor-Based算法的学习笔记
- Apache SeaTunnel(Incubating) 2.2.0-beta 版本发布!API 重构,连接器与引擎解偶
- 学iOS开发需要什么样的基础?
- deepfool简单实现
- b站修改密码服务器错误,提示账号或者密码错误,无法正常登陆
- 【Coding】Latex添加表格注释footnote
- 微信打开链接被拦截怎么处理 如何预防微信中域名投诉
- pikachu之xss漏洞学习
- Unity 中实现子弹时间效果
- APP端接入支付宝支付接口
热门文章
- 利用jasperreports报表生成pdf文档中文不能显示问题解决方法
- 经纬度,墨卡托等坐标转换
- Mysql的limit用法
- WIN 10进入休眠、睡眠、关机的命令
- Kotlin 1.2 有哪些新特性
- Linux远程桌面工具Xming+Putty的搭建
- [翻译] SoundManager 音频管理器
- 2002-2003 ACM-ICPC Northeastern European Regional Contest (NEERC 02) A Amusing Numbers (数学)
- Rails源码笔记-ActiveSupport-core_ext-array
- sequel pro mac_Mac手绘插画绘图软件推荐