android解析json,Android解析json
android json解析成map格式
先看json数据
成都创新互联技术团队10年来致力于为客户提供成都网站建设、做网站、高端网站设计、成都全网营销、搜索引擎SEO优化等服务。经过多年发展,公司拥有经验丰富的技术团队,先后服务、推广了千余家网站,包括各类中小企业、企事单位、高校等机构单位。
之前使用的是gson解析,把 discount 写成Object格式,但是通过解析之后转成String类型,成为
这种明显没有办法解析成map集合
使用原始的Json解析,一层一层的找到这个对象,转成String类型成为
这种情况下就可以了 之后是copy的解析方法
第一种方法是
第二种是
这样就可以获取到map集合了
android中这样的JSON数据应该如何解析,
//json我的理解就是一系列键值对,只要KEY值对了,对应数据格式也对了,那就可以了!~
JsonObject j = ...//此处是获取json的方法,自定
boolean b = j.getBoolean("result");//取出“result”对应的值,这里的结果应该
//是"true";
JsonArray ja = j.getJsonArray("asks");//取出“asks”对应的值,因为asks对应的值
//中含有方括号,所以这是JsonArray型数据,需要取出进一步解析;JsonArray可以与数组
//进行比较理解,我个人是这样的,供参考
for(int i = 0; i ja.length(); i++){
JsonArray ja1 = ja.getJsonArray(i);//获取ja中的第一个元素,因为这个元素
//也是jsonArray,所以可以再进一步解析
double d1 = ja1.getDouble(0);//jsonArray中没有getFloat()这个方法
//,自行转型就可以了
double d2 = ja1.getDouble(1);//
} 以上,希望对你有所帮助
Android 解析json问题
Android 解析json的方式为:
1、首先,搭建一个服务器的工程:JsonProject这个项目
源代码:
Person.java
package com.json.domain;
public class Person {
private int id;
private String name;
private String address;
public Person() {
super();
}
public Person(int id, String name, String addrss) {
super();
this.id = id;
this.name = name;
this.address = addrss;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Person [addrss=" + address + ", id=" + id + ", name=" + name
+ "]";
}
}
JsonService.java
package com.json.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.json.domain.Person;
public class JsonService {
public JsonService() {
}
public Person getPerson(){
Person person = new Person(1001,"jack","上海黄浦区");
return person;
}
public ListPerson getListPerson(){
ListPerson list = new ArrayListPerson();
Person person1 = new Person(1001,"jack","上海黄浦区");
Person person2 = new Person(1002,"rose","上海闵行区");
Person person3 = new Person(1003,"mick","上海黄浦区");
list.add(person1);
list.add(person2);
list.add(person3);
return list;
}
public ListString getListString(){
ListString list = new ArrayListString();
list.add("北京");
list.add("上海");
list.add("湖南");
return list;
}
public ListMapString,Object getListMaps(){
ListMapString,Object list = new ArrayListMapString,Object();
MapString,Object map1 = new HashMapString, Object();
MapString,Object map2 = new HashMapString, Object();
map1.put("id", 1001);
map1.put("name", "jack");
map1.put("address", "北京");
map2.put("id", 1001);
map2.put("name", "rose");
map2.put("address", "上海");
list.add(map1);
list.add(map2);
return list;
}
}
JsonServlet.java
package com.json.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.json.service.JsonService;
import com.json.tools.JsonTools;
public class JsonServlet extends HttpServlet {
private JsonService service;
/**
* Constructor of the object.
*/
public JsonServlet() {
super();
}
/**
* Destruction of the servlet. br
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. br
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* The doPost method of the servlet. br
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String jsonString = "";
String action_flag = request.getParameter("action_flag");
if(action_flag.equals("person")){
jsonString = JsonTools.createJsonString("person", service.getPerson());
}else if(action_flag.equals("persons")){
jsonString = JsonTools.createJsonString("persons", service.getListPerson());
}else if(action_flag.equals("listString")){
jsonString = JsonTools.createJsonString("listString", service.getListString());
}else if(action_flag.equals("listMap")){
jsonString = JsonTools.createJsonString("listMap", service.getListMaps());
}
out.println(jsonString);
out.flush();
out.close();
}
/**
* Initialization of the servlet. br
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
service = new JsonService();
}
}
2、通过浏览器
访问地址一:
输出以下结果:
{"person":{"address":"上海黄浦区","id":1001,"name":"jack"}
访问地址二:
输出以下结果:
{"persons":[{"address":"上海黄浦区","id":1001,"name":"jack"},{"addrss":"上海闵行区","id":1002,"name":"rose"},{"address":"上海黄浦区","id":1003,"name":"mick"}]}
访问地址三:
输出以下结果:
{"persons":["北京","上海","湖南"]}
访问地址四:
输出以下结果:
{"persons":[{"id":1001,"address":"北京","name":"jack"},{"id":1001,"address":"上海","name":"rose"}]}
android json解析三种方式哪种效率最高
用org.json以及谷歌提供gson来解析json数据的方式更好一些。
安卓下通常采用以下几种方式解析json数据:
1、org.json包(已经集成到android.jar中了)
2、google提供的gson库
3、阿里巴巴的fastjson库
4、json-lib
以Google出品的Gson为例,具体步骤为:
1、首先,从 code.google.com/p/google-gson/downloads/list下载GsonAPI:
google-gson-1.7.1-release.zip 把gson-1.7.jar copy到libs(项目根目录新建一个libs文件夹)中。 可以使用以下两种方法解析JSON数据,通过获取JsonReader对象解析JSON数据。
代码如下:
String jsonData = "[{\"username\":\"arthinking\",\"userId\":001},{\"username\":\"Jason\",\"userId\":002}]";
try{
JsonReader reader = new JsonReader(new StringReader(jsonData));
reader.beginArray();
while(reader.hasNext()){
reader.beginObject();
while(reader.hasNext()){
String tagName = reader.nextName();
if(tagName.equals("username")){
System.out.println(reader.nextString());
}
else if(tagName.equals("userId")){
System.out.println(reader.nextString());
}
}
reader.endObject();
}
reader.endArray();
}
catch(Exception e){
e.printStackTrace();
}
2、使用Gson对象获取User对象数据进行相应的操作:
代码如下:
Type listType = new TypeTokenLinkedListUser(){}.getType();
Gson gson = new Gson();
LinkedListUser users = gson.fromJson(jsonData, listType);
for (Iterator iterator = users.iterator(); iterator.hasNext();) {
User user = (User) iterator.next();
System.out.println(user.getUsername());
System.out.println(user.getUserId());
}
3、如果要处理的JSON字符串只包含一个JSON对象,则可以直接使用fromJson获取一个User对象:
代码如下:
String jsonData = "{\"username\":\"arthinking\",\"userId\":001}";
Gson gson = new Gson();
User user = gson.fromJson(jsonData, User.class);
System.out.println(user.getUsername());
System.out.println(user.getUserId());
当前题目:android解析json,Android解析json
本文网址:http://scyanting.com/article/dsicgih.html