Json的生成和解析
json是常见的数据格式,生成和解析是常用的操作。Android中,默认提供orgJson供我们使用,除此之外,google也提供了Gson库方便我们开发。
Json样例类
package com.fxb.jsontest; import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import com.google.gson.Gson;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; public class JsonUtils { public static String createJson(){
try {
JSONObject json = new JSONObject();
json.put("key1", "value1");
json.put("key2", "value2"); JSONObject json1 = new JSONObject();
json1.put("key3", "value3");
json1.put("key4", "value4");
json.put("child1", json1); JSONObject json2 = new JSONObject();
json2.put("key5", "value5");
json2.put("key6", "value6");
json1.put("child2", json2); JSONObject json3 = new JSONObject();
json3.put("key7", "value7");
json3.put("key8", "value8");
json2.put("child2", json3); JSONArray arr = new JSONArray();
JSONObject json4 = new JSONObject();
json4.put("key9", "value9");
json4.put("key10", "value10");
arr.put(json4); JSONObject json5 = new JSONObject();
json5.put("key11", "value11");
json5.put("key12", "value12");
arr.put(json5); json3.put("child3", arr); // Log.i("JsonTest", json.toString());
return json.toString();
} catch (JSONException e) {
e.printStackTrace();
return null;
}
} public static void orgJsonTest(Context context){
String str = "{\"key1\":\"value1\",\"key2\":\"value2\",\"child1\":{\"key3\":\"value3\",\"key4\":\"value4\",\"child2\":{\"key5\":\"value5\",\"key6\":\"value6\",\"child3\":{\"key7\":\"value7\",\"key8\":\"value8\",\"child4\":[{\"key9\":\"value9\",\"key10\":\"value10\"},{\"key11\":\"value11\",\"key12\":\"value12\"}]}}}}";
try {
JSONObject jsonRoot = new JSONObject(str);
JSONObject jsonChild3 = jsonRoot.getJSONObject("child1").getJSONObject("child2").getJSONObject("child3");
if(jsonChild3 != null){
String str7 = jsonChild3.getString("key7");
String str8 = jsonChild3.getString("key8"); JSONArray jsonArr = jsonChild3.getJSONArray("child4");
JSONObject jsonChild4_1 = jsonArr.getJSONObject(0);
String str9 = jsonChild4_1.getString("key9");
String str10 = jsonChild4_1.getString("key10"); // Log.i(MainActivity.TAG, "str7:"+str7+", str8:"+str8);
// Log.i(MainActivity.TAG, "str9:"+str9+", str10:"+str10);
String strShow = "str7:"+str7+", str8:"+str8 + "\n"+"str9:"+str9+", str10:"+str10;
showToast(context, strShow);
}
else{
Log.i(MainActivity.TAG, "empty");
}
} catch (JSONException e) {
e.printStackTrace();
}
} public static void gsonTest(Context context){
Gson gson = new Gson();
int i = gson.fromJson("100", int.class);
double d = gson.fromJson("1.234", double.class);
boolean b = gson.fromJson("true", boolean.class); final String strPerson = "{\"id\":5678,\"name\":\"Jack\",\"isWell\":\"true\",\"adress\":\"beijing\"}"; Person person = gson.fromJson(strPerson, Person.class);
Toast.makeText(context, "i:" + i + "\nd:" + d + "\nb:" + b + "\nperson\n" + person.show(), Toast.LENGTH_SHORT).show(); } public static void showToast(Context context, String str){
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
} public static class Person{
private int id;
private String name;
private boolean isWell;
private String adress; public String show(){
return "id="+id+", isWell="+ isWell +", adress="+adress;
}
} }
测试Activity类
package com.fxb.jsontest; import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button; public class MainActivity extends Activity implements View.OnClickListener { public static final String TAG = "JsonTest";
private Button btnCreateJson, btnOrgJson, btnGson, btnFastJson; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
} private void initView(){
btnCreateJson = (Button)findViewById(R.id.btnCreateJson);
btnOrgJson = (Button)findViewById(R.id.btnOrgJson);
btnGson = (Button)findViewById(R.id.btnGson);
btnFastJson = (Button)findViewById(R.id.btnFastJson); btnCreateJson.setOnClickListener(this);
btnOrgJson.setOnClickListener(this);
btnGson.setOnClickListener(this);
btnFastJson.setOnClickListener(this);
} @Override
public void onClick(View v) {
if(v == btnCreateJson){
String str = JsonUtils.createJson();
Log.i(TAG, str);
}
else if(v == btnOrgJson){
JsonUtils.orgJsonTest(this);
}
else if(v == btnGson){ }
else if(v == btnFastJson){ }
}
}
样例中,成功创建了json字符串,并实现了解析。
Json的生成和解析的更多相关文章
- java中json数据生成和解析(复杂对象演示)
1.json简单介绍 1.1 json是最流行和广泛通用的数据传输格式,简称JavaScript Object Notation,最早在JavaScript中使用. 1.2 举个例子,下面是一个jso ...
- QT json字符串生成和解析
1 QT json字符串生成和解析 1.1 QT Json解析流程 (1) 字符串转化为QJsonDocument QJsonParseError json_error; QJso ...
- java json 的生成和解析 --json-lib
类(java json的解析和生成): import java.util.HashMap; import java.util.Map; import net.sf.json.JSONArray; im ...
- Qt之JSON生成与解析
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - December ...
- 【转载】Qt之JSON生成与解析
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - December ...
- iOS开发之JSON格式数据的生成与解析
本文将从四个方面对IOS开发中JSON格式数据的生成与解析进行讲解: 一.JSON是什么? 二.我们为什么要用JSON格式的数据? 三.如何生成JSON格式的数据? 四.如何解析JSON格式的数据? ...
- fastjson生成和解析json数据,序列化和反序列化数据
本文讲解2点: 1. fastjson生成和解析json数据 (举例:4种常用类型:JavaBean,List<JavaBean>,List<String>,List<M ...
- fastjson生成和解析json数据
本文讲解2点: 1. fastjson生成和解析json数据 (举例:4种常用类型:JavaBean,List<JavaBean>,List<String>,List<M ...
- Android Json生成及解析实例
JSON的定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据 ...
随机推荐
- loadrunner 脚本优化-事务时间简介
脚本优化-事务时间简介 by:授客 QQ:1033553122 事务概念 事务是指用户在客户端做一种或多种业务所需要的操作集(actions),通过事务开始和结束函数可以标记完成该业务所需要的操作内容 ...
- Android为TV端助力 切换fragment的两种方式
使用add方法切换时:载入Fragment1Fragment1 onCreateFragment1 onCreateViewFragment1 onStartFragment1 onResume用以下 ...
- WiFi的名词缩写
http://blog.csdn.net/jayxujia123/article/details/12842295 无线网络最初采用的安全机制是WEP(有线等效私密),但是后来发现WEP是很不安全的, ...
- spring Boot 出现:org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplication ...
- git上传中的排除的配置文件, git实际的操作代码;
git上传中的排除的配置文件: git实际的操作 在主目录建立.gitignore文件并输入以下保存: *.class #package file *.war *.ear #kdiff3 ignore ...
- 记录一次spark连接mysql遇到的问题
版权声明:本文为博主原创文章,未经博主允许不得转载 在使用spark连接mysql的过程中报错了,错误如下 08:51:32.495 [main] ERROR - Error loading fact ...
- Linux CPU占用率监控工具小结
关键词:top.perf.sar.ksar.mpstat.uptime.vmstat.pidstat.time.cpustat.munin.htop.glances.atop.nmon.pcp-gui ...
- NSJSONSerialization 反序列化失败 NSCocoaErrorDomain Code=3840
NSJSONSerialization 反序列化失败 NSCocoaErrorDomain Code=3840 NSCocoaErrorDomain Code=3840 “No string key ...
- C# 中将月份格式化为英语缩写格式
在测试Android 系统的时候,日期输入框需要输入英语短格式,如下. 考虑到系统日期格式和地域的关系紧密,地域不同,日期格式不同,所以经过查找,找到下面的解决方法. date.ToString(&q ...
- Socket引子
=== ''' Socket网络编程: --应用层:http smtp dns ftp ssh snmp dhcp... 无论协议是什么本质上都是数据交换,总结为两种方式:收和发 --传输层(端口Po ...