ANDROID_MARS学习笔记_S02_013_Gson解析json串
1.MainActivity.java
package com.json; import java.io.IOException;
import java.io.StringReader; import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.JsonReader;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity { private String jsonData = "[{\"name\":\"Michael\",\"age\":20},{\"name\":\"Mike\",\"age\":21}]";
private Button button ; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.buttonId);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
JsonUtils jsonUtils = new JsonUtils();
jsonUtils.parseJson(jsonData);
}
});
} class JsonUtils {
@SuppressLint("NewApi")
public void parseJson(String jsonData) {
JsonReader reader = new JsonReader(new StringReader(jsonData));
try {
reader.beginArray();//开始解析数组
while (reader.hasNext()) {
reader.beginObject();//开始解析对象
while (reader.hasNext()) {//开始解析键值对
String tagName = reader.nextName();
if(tagName.equals("name")) {
System.out.println("name-->" + reader.nextString());
}
else if(tagName.equals("age")) {
System.out.println("age--->" + reader.nextInt());
}
}
reader.endObject();
}
reader.endArray();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
ANDROID_MARS学习笔记_S02_013_Gson解析json串的更多相关文章
- ANDROID_MARS学习笔记_S02_015_Gson解析json串为对象集合
package com.example.s02_e12_json3; import java.lang.reflect.Type; import java.util.Iterator; import ...
- ANDROID_MARS学习笔记_S02_014_GSON解析JSON串为对象
package com.json2; import android.app.Activity; import android.os.Bundle; import android.view.View; ...
- 怎么解析json串在.net中
以前知道一种解析json串的方法,觉得有点麻烦.就从别的地方搜到了另一种 string json = vlt.getlist(); JObject jo = JObject.Parse(json); ...
- 在.net中怎么解析json串 [Error reading JObject from JsonReader. Current JsonReader item is not an obj]
编辑时间:2017-05-10,增加一种转化list的方法 一.以前知道一种解析json串的方法,觉得有点麻烦.就从别的地方搜到了另一种 string json = vlt.getlist(); JO ...
- C++ 解析json串
首先, C++ 解析json串,需要用到第三方库(json_vc71_libmtd.lib).然后,VS2010,创建项目json_read,配置项目属性.最后,拷贝下面的代码就可以看到效果了. #i ...
- 前端学习之——js解析json数组
** 前端学习之——js解析json数组** 解析json数组即对JSONArray的遍历 一.对于标准的json数组如: var result=[{"flag":1," ...
- SpringMVC Jackson 库解析 json 串属性名大小写自动转换问题
问题描述 在项目开发中,当实体类和表中定义的某个字段为 RMBPrice,首字母是大写的,sql 查询出来的列名也是大写的 RMBPrice,但是使用 jquery 的 ajax 返回请求响应时却出错 ...
- Android学习笔记之Fast Json的使用
PS:最近这两天发现了Fast Json 感觉实在是强大.. 学习内容: 1.什么是Fast Json 2.如何使用Fast Json 3.Fast Json的相关原理 4.Fast Json的优势, ...
- java解析json串获取key和value
网上例子巨多,纯属个人笔记: JSONObject maleArray = maleObject.getJSONObject("extension"); Iterator<S ...
随机推荐
- Java read()和readLine()的区别
1.read() 功能:读取单个字符的个数,如果已经读完的话会返回-1 (其范围从 0 到 65535 ) 例子如下: byte[] buf = new byte[1024]; int len; wh ...
- String filePath = request.getSession().getServletContext().getRealPath("/");这句话返回的路径是什么,解释下getRealPath("/")函数中的"/"表示什么意思
request.getSession().getServletContext() 获取的是Servlet容器对象,相当于tomcat容器了.getRealPath("/") 获取实 ...
- Highcharts在IE中不能一次性正常显示的一种解决办法
由于客户要求必须在IE浏览器下兼容图表,故选用了兼容性较好的Highcharts.另外说一句,博主尝试过ichartjs.ECharts.YUI,兼容性都没有Highcharts给力(所有的兼容性问题 ...
- MVC小系列(十五)【MVC+ZTree实现对树的CURD及拖拽操作】
根据上一讲的可以加载一棵大树,这讲讲下如果操作这颗大树 <link href="../../Scripts/JQuery-zTree/css/zTreeStyle/zTreeStyle ...
- DataContext 数据在F5刷新频繁,会出现数据读取错误
DataContext 数据在F5刷新频繁,会出现数据读取错误 DataContext是 Linq to sql数据模型的底层数据库对象所有LInq数据表对象都是由它派生的, 只要建立一个数据库操作, ...
- 【JAVA错误笔记】 - 【Could not open ServletContext resource [/WEB-INF/applicationContext.xml]解决方法】
错误描述: Could not open ServletContext resource [/WEB-INF/applicationContext.xml] 原因分析: 问题主要由于加载spring的 ...
- spring boot 配置文件提示自定义配置属性
1.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- 利用Highcharts制作web图表学习(一)
前言:最近项目中需要对数据进行汇总和分析得出柱状图或曲线图这样散装点图,经过学习和测试发现Highchart不错,如果大家项目中需要的话,不妨看看有的例子摘自官网 一.先说说HighCharts的 ...
- WPF动画之关键帧动画(2)
XAML代码: <Window x:Class="关键帧动画.MainWindow" xmlns="http://schemas.microsoft.com/win ...
- C#获取时间戳的问题
最近在做一个接口,需要用到时间戳,在请求接口时,返回超时,接口方的技术称是时间戳的不对(超出一定范围[比如1分钟]就返回超时)导致的. 首先,看代码: public static double Get ...