版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
2021国家开放大学电大《Android网络开发技术》
形考任务一教学实践1答案
形考任务一教学实践1
完成以下JSON的解析,完成主体代码即可,解析方法及库不限。
一、什么是JSON?
JSON是一种取代XML的数据结构,和xml相比,它更小巧但描述能力却不差,由于它的小巧所
以网络传输数据将减少更多流量从而加快速度。
JSON就是一串字符串只不过元素会使用特定的符号标注。
{}双括号表示对象
[]中括号表示数组
""双引号内是属性或值
:冒号表示后者是前者的值(这个值可以是字符串、数字、也可以是另一个数组或对象)
所以{"name":"Michael")可以理解为是一个包含name为Michael的对象
W[{"name":"Michael"),{"name":"Jerry"}]就表示包含两个对象的数组
当然了,你也可以使用{"name":["Michael","Jerry"]}来简化上面一部,这是一个拥有一个name
数组的对象
二、JSON解析之传统的JSON解析
1、生成json字符串
publicstaticStringcreateJsonString(Stringkey,Objectvalue){
JSONObjectjsonObject=newJSONObject();
jsonObject.put(key,value);
returnjsonObject.toString();
}
2、解析JSON字符串
分为以下三种情况,一个JavaBean,一个List数组,一个嵌套Map的List数组:
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.Iterator;
importjava.util.List;
importjava.util.Map;
importorg.json.JSONArray;
importorg.json.JSONObject;
importcom.android.myjson.domain.Person;
/**
*完成对json数据的解析
*
*/
publicclassJsonTools{
publicstaticPersongetPerson(Stringkey,StringjsonString){
Personperson=newPerson();
try(
JSONObjectjsonObject=newJSONObject(jsonString);
JSONObjectpersonobject=jsonObject.getJSONObject("person");
person.setld(personObject.getlnt("id"));
person.setName(personObject.getString("name"));
person.setAddress(personObject.getString("address"));
}catch(Exceptione){
//TODO:handleexception
)
returnperson;
)
publicstaticListgetPersons(Stringkey,StringjsonString){
Listlist=newArrayList();
try(
JSONObjectjsonObject=newJSONObject(jsonString);
//返回json的数组
JSONArrayjsonArray=jsonObject.getJSONArray(key);
for(inti=0;i<jsonArray.length();i++){
JSONObjectjsonObject?=jsonArray.getJSONObject(i);
Personperson=newPerson();
person.setld(jsonObject2.getlnt("id"));
person.setName(jsonObject2.getString("name"));
person.setAddress(jsonObject2.getString("address"));
list.add(person);
)
}catch(Exceptione){
//TODO:handleexception
)
returnlist;
)
publicstaticListgetList(Stringkey,StringjsonString){
Listlist=newArrayList();
try(
JSONObjectjsonObject=newJSONObject(jsonString);
JSONArrayjsonArray=jsonObject.getlSONArray(key);
for(inti=0;i<jsonArray.length();i++){
Stringmsg=jsonArray.getString(i);
list.add(msg);
)
}catch(Exceptione){
//TODO:handleexception
)
returnlist;
}
publicstaticList>listKeyMaps(Stringkey,
StringjsonString){
List>list=newArrayList>();
try(
JSONObjectjsonObject=newJSONObject(jsonString);
JSONArrayjsonArray=jsonObject.getlSONArray(key);
for(inti=0;i<jsonArray.length();i++){
JSONObjectjsonObject?=jsonArray.getJSONObject(i);
Mapmap=newHashMap();
Iteratoriterator=jsonObject?.keys();
while(iterator.hasNext()){
Stringjson_key=iterator.next();
Objectjson__value=jsonObject2,get(json_key);
if(json_value==null){
json_value=
}
map.put(json_keyzjson__value);
}
list.add(map);
}
}catch(Exceptione){
//TODO:handleexception
}
returnlist;
}
}
三、JSON解析之GSON
1、生成JSON字符串
importcom.google.gson.Gson;
publicclassJsonlltils{
publicstaticStringcreateJsonObject(Objectobj){
Gsongson=newGson();
Stringstr=gson.toJson(obj);
returnstr;
)
)
二、解析JSON
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Map;
importcom.google.gson.Gson;
importcom.google.gson.reflect.Typelbken;
/
publicclassGsonTools{
publicGsonTools(){
//TODOAuto-generatedconstructorstub
)
/**
*@param
*@paramjsonString
*@paramcis
*@return
*/
publicstaticTgetPerson(StringjsonString,Classcis){
Tt=null;
try(
Gsongson=newGson();
t=gson.fromJson(jsonString,cis);
}catch(Exceptione){
//TODO:handleexception
)
returnt;
)
/**
*使用Gson进行解析List
*
*@param
*@paramjsonString
*@paramcis
*@return
*/
publicstaticListgetPersons(StringjsonString,Classcis){
Listlist=newArrayList();
try(
Gsongson=newGson();
list=gson.fromJson(jsonString,newTypeToken>(){
}.getType());
}catch(Exceptione){
)
returnlist;
)
/**
*@paramjsonString
*@return
*/
publicstaticListgetList(StringjsonString){
Listlist=newArrayList();
try(
Gsongson=newGson();
list=gson.fromJson(jsonString,newTypeToken>(){
}.getType());
}catch(Exceptione){
//TODO:handleexception
)
returnlist;
)
publicstaticList>listKeyMaps(StringjsonString){
List>list=newArrayUst>();
try(
Gsongson=newGson();
list=gson.fromJson(jsonString,
newTypeToken»(){
}.getType());
}catch(Exceptione){
//TODO:handleexception
)
returnlist;
三、JSON解析之FastJSON
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Map;
importcom.alibaba.fastjsonJSON;
importcom.alibaba.fastjson.TypeReference;
publicclassJsonTool{
publicstaticTgetPerson(Stringjsonstring,Classcis){
Tt=null;
try(
t=JSON.parseObject(jsonstring,cis);
}catch(Exceptione){
//TODO:handleexception
)
returnt;
}
publicstaticListgetPersonList(Stringjsonstring,Classcis){
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 海南电信消防安全云课堂
- 高中地理易混淆地理概念辨析别再混淆了
- 英语四年级下册Unit 2 Family Rules 教案
- 阅卷评分标准与细则
- 公关服务公司公关服务技能专项培训管理制度
- 2026电商插画面试题库及答案
- 2026东阳科学面试题目及答案
- 工业机器人应用开发协议(2026年科技公司)
- 常见肿瘤标志物重点2026
- 电气安装工程质量验收规范手册
- 《相见欢无言独上西楼》课件
- 浓硫酸泄漏应急预案
- 广东省普通高中学生档案
- DB13T 5714-2023 道路运输企业安全生产风险分级管控规范
- 华中科技大学研究生入学考试组织行为学
- 濮良贵机械设计课件完整版
- RB/T 024-2019合格评定服务认证技术应用指南
- GB/T 4010-2015铁合金化学分析用试样的采取和制备
- GA/T 832-2014道路交通安全违法行为图像取证技术规范
- 输电线路工程组塔施工质量控制
- 公共伦理学(第三版)-课件
评论
0/150
提交评论