导航:首页 > 操作系统 > android自带json解析

android自带json解析

发布时间:2023-07-30 02:11:35

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 List<Person> getListPerson(){
List<Person> list = new ArrayList<Person>();
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 List<String> getListString(){
List<String> list = new ArrayList<String>();
list.add("北京");
list.add("上海");
list.add("湖南");
return list;
}

public List<Map<String,Object>> getListMaps(){
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
Map<String,Object> map1 = new HashMap<String, Object>();
Map<String,Object> map2 = new HashMap<String, 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、通过浏览器
访问地址一:http://wulianghuan-pc:8080/JsonProject/servlet/JsonServlet?action_flag=person
输出以下结果:
{"person":{"address":"上海黄浦区","id":1001,"name":"jack"}
访问地址二:http://wulianghuan-pc:8080/JsonProject/servlet/JsonServlet?action_flag=persons
输出以下结果:
{"persons":[{"address":"上海黄浦区","id":1001,"name":"jack"},{"addrss":"上海闵行区","id":1002,"name":"rose"},{"address":"上海黄浦区","id":1003,"name":"mick"}]}
访问地址三:http://wulianghuan-pc:8080/JsonProject/servlet/JsonServlet?action_flag=listString
输出以下结果:
{"persons":["北京","上海","湖南"]}
访问地址四:http://wulianghuan-pc:8080/JsonProject/servlet/JsonServlet?action_flag=listMap
输出以下结果:
{"persons":[{"id":1001,"address":"北京","name":"jack"},{"id":1001,"address":"上海","name":"rose"}]}

Ⅱ android 用JSON 解析数据接口方法

接口获取下来的数据为

{"status":200,"message":"查询成功","data":{"id":32,"user_id":null,"user_code":null,"user_pass":null,"meeting_id":"1","meeting_pass":null,"config_param_ip":"11.1.1.70","port":"6501","web_config_param_ip":"11.1.1.70","web_port":"6501","unit_code":null,"create_time":null,"update_time":null,"status":0,"userCode":"video1","userPass":"video1","meetingId":"1","meetingPass":"","apiPort":"13000","username":"admin","password":"123456","updateTime":"2021-11-08 17:45:29"}}

使用方法

try {

    JSONObject jsonObject = new JSONObject(outstring);

    int resultCode = jsonObject.getInt("status");

    if (resultCode == 200) {

        JSONObject obj = jsonObject.getJSONObject("data");

        IP = obj.getString("config_param_ip");

        port = obj.getString("web_port");

        username = obj.getString("userCode");

        password = obj.getString("userPass");

        roomID = Integer.parseInt(obj.getString("meetingId"));

        roomPassword = obj.getString("password");

    } else {

        ToastUtils.showShort("查询失败");

    }

    goVideo();

} catch (Exception e) {

    e.printStackTrace();

}

Ⅲ android怎么样获取并解析json数据

json数据 是从接口获取来的 其实就是一串字符串 可以用Gson解析。
Gson gson =new Gson( ),然后调用fromJson 方法解析

Ⅳ android中这样的JSON数据应该如何解析,

//json我的理解就是一系列键值对,只要KEY值对了,对应数据格式也对了,那就可以了!~
JsonObjectj=...//此处是获取json的方法,自定
booleanb=j.getBoolean("result");//取出“result”对应的值,这里的结果应该
//是"true";
JsonArrayja=j.getJsonArray("asks");//取出“asks”对应的值,因为asks对应的值
//中含有方括号,所以这是JsonArray型数据,需要取出进一步解析;JsonArray可以与数组
//进行比较理解,我个人是这样的,供参考
for(inti=0;i<ja.length();i++){

JsonArrayja1=ja.getJsonArray(i);//获取ja中的第一个元素,因为这个元素
//也是jsonArray,所以可以再进一步解析
doubled1=ja1.getDouble(0);//jsonArray中没有getFloat()这个方法
//,自行转型就可以了
doubled2=ja1.getDouble(1);//

}

以上,希望对你有所帮助

阅读全文

与android自带json解析相关的资料

热点内容
怎么加速python计算素数 浏览:238
腰椎第五节压缩性骨折 浏览:89
程序员开会的句子 浏览:990
用哪个app写编程 浏览:643
android通讯录增删改查 浏览:727
车贷解压过户可以同时进行吗 浏览:917
java面向对象编程题目 浏览:876
二次元压缩包 浏览:698
stc仿真器编程器 浏览:155
服务器销售怎么做好 浏览:87
什么是com编程 浏览:848
算法工程师最新资讯 浏览:608
邮政银行卡怎么在app签约绑定 浏览:49
压缩卷一直转 浏览:976
初一编程小程序怎么做 浏览:827
bt软件文件夹名称 浏览:158
unix创建命令 浏览:624
devc是多少位的编译器 浏览:982
怎么样能快点升安卓系统 浏览:977
奇迹mu用什么服务器 浏览:606