当前位置:首页 » 安卓系统 » androidjson串解析

androidjson串解析

发布时间: 2023-05-06 07:47:45

Ⅰ Android 解析json问题

java">///http地址
StringhttpUrl=ip+":"+端口号+"/loginbyandroid/validate.do";
//HttpPost连接对象
HttpPosthttpRequest=newHttpPost(httpUrl);
//使用NameValuePair来保存要传递的Post参数
List<NameValuePair>params=newArrayList<NameValuePair>();
//添加要传递的参数
params.add(newBasicNameValuePair("loginId","value"));
params.add(newBasicNameValuePair("password","value"));
//设置字符集
HttpEntityhttpentity;
try{
httpentity=newUrlEncodedFormEntity(params,"utf-8");

//请求httpRequest
httpRequest.setEntity(httpentity);
//取得默认的HttpClient
HttpClienthttpclient=newDefaultHttpClient();
//取得HttpResponse
HttpResponsehttpResponse;
httpResponse=httpclient.execute(httpRequest);
//HttpStatus.SC_OK表示连接成功
if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
//取得返回的字符串
StringstrResult=EntityUtils.toString(httpResponse
.getEntity());
JSONArrayjsonArray=newJSONArray(strResult);
for(inti=0;i<jsonArray.length();i++){
JSONObjectjsonObject=(JSONObject)jsonArray.opt(i);
Stringsuccess=jsonObject.getString("success");
StringJSESSIONID=jsonObject.getString("JSESSIONID");
StringloginName=jsonObject.getString("loginName");
Stringorgname=jsonObject.getString("orgname");
System.out.println("success="+success
+"JSESSIONID="+JSESSIONID+"loginName="
+loginName+"orgname="+orgname);
}
}else{
System.out.println("请求错误!");
}
}catch(ClientProtocolExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}

Ⅱ 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( JavaScript Object Notation ) 是一种轻量级的数据交换格式。易于阅读和编写,同时也易于机器解析和生成。

JSON 建构于两种结构:

JSON 具有以下这些格式:

参考: Android 中 解析 JSON

Android 提供类四种不同的类来操作 JSON 数据。这些类是 JSONArray、JSONObject、JSONStringer 和 JSONTokenizer

为了解析 JSON 对象,须先创建一个 JSONObject 类的对象,需要传入需解析的字符串 JSONObject root = new JSONObject(candyJson); 然后根据 JSONObject 对象提供方法以及数据类型解析对应 json 数据。下表展示一些 JSONObiect 提供的方法

示例:

Ⅳ Android怎么解析这个json



importjava.util.Iterator;
importjava.util.Map.Entry;
importjava.util.Set;

importcom.google.gson.JsonArray;
importcom.google.gson.JsonElement;
importcom.google.gson.JsonObject;
importcom.google.gson.JsonParser;

publicclassJU
{
publicstaticvoidmain(String[]args)
{
Stringjson=
"{"showapi_res_code":0,"showapi_res_error":"","showapi_res_body":{"ret_code":0,"basic":{"explains":["n.技术;工艺;术语"]}}}";
JsonParserparser=newJsonParser();
JsonObjectobj=(JsonObject)parser.parse(json);
System.out.println(obj);
dir(obj);
}

privatestaticvoiddir(Objectobj)
{
if(objinstanceofJsonObject)
{
JsonObjectobject=(JsonObject)obj;
Set<Entry<String,JsonElement>>entry=object.entrySet();
Iterator<Entry<String,JsonElement>>it=entry.iterator();
while(it.hasNext())
{
Entry<String,JsonElement>elem=it.next();
Stringkey=elem.getKey();
JsonObjectoob;
try
{
oob=(JsonObject)elem.getValue();
System.out.println(key+"--->"+oob);
dir(oob);
}
catch(Exceptione)
{
JsonElementelement=elem.getValue();
System.out.println(key+"--->"+element);
dir(element);
}
}
}
elseif(objinstanceofJsonArray)
{
JsonArrayarray=(JsonArray)obj;
for(inti=0;i<array.size();i++)
{
JsonElementelement=array.get(i);
System.out.print(element+"");
dir(element);
}
}
}
}

Ⅳ android中使用JAVA解析json数据

这个格式使用如下代码解析

try{
JSONObjectjsonObject=newJSONObject(json);
JSONObjectweatherinfo=jsonObject.getJSONObject("weatherinfo");

System.out.println(weatherinfo.getString("city"));
System.out.println(weatherinfo.getString("cityid"));
System.out.println(weatherinfo.getString("temp"));
System.out.println(weatherinfo.getString("WD"));

}catch(JSONExceptione){
e.printStackTrace();
}
其中第一行代码JSONObjectjsonObject=newJSONObject(json);//json即为你的字符串

比如现在天气信息是多个城市的,并非只有北京市。
{"weatherinfo":[{"city":"北京","cityid":"101010100","temp":"4","WD":"东风","WS":"2级","SD":"75%","WSE":"2","time":"10:45","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB","njd":"暂无实况","qy":"1011"},{"city":"天津","cityid":"101010100","temp":"4","WD":"东风","WS":"2级","SD":"75%","WSE":"2","time":"10:45","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB","njd":"暂无实况","qy":"1011"}]}
以下代码适用。

try{
JSONObjectjsonObject=newJSONObject(json);
JSONArrayjsonArray=jsonObject.getJSONArray("weatherinfo");
for(inti=0;i<jsonArray.length();i++){
JSONObjectobject=jsonArray.getJSONObject(i);
System.out.println(object.getString("city"));
System.out.println(object.getString("cityid"));
System.out.println(object.getString("temp"));
System.out.println(object.getString("WD"));
}
}catch(JSONExceptione){
e.printStackTrace();
}

Ⅵ 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 到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 TypeToken<LinkedList<User>>(){}.getType();
Gson gson = new Gson();
LinkedList<User> 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数据应该如何解析,

//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数据怎么解析

先将字符串转换为json对象或数组
这段字符串中以]结尾为数组
JSonArray array = new JSonArray
遍历这个数组获得对应的对象
for(int i = 0 ; i < array.length() ;i++){
JSONObject obj = array.getJSONObject(i);

}

Ⅸ android怎样解析复杂JSON数据,层数比较多的

1使用android内置的org.json包提供的类解析,并保存在指定的对象中
Response response = new Response();
// 解析response_head
Response_head response_head = new Response_head();
JSONObject jsonObject = new JSONObject(json);
JSONObject responseHead = jsonObject.getJSONObject("response_head");
response_head.setRespmenu(responseHead.getString("respmenu"));
response_head.setResptime(responseHead.getString("resptime"));
Respinfo respInfo = new Respinfo();
JSONObject respinfo = responseHead.getJSONObject("respinfo");
respInfo.setRespcode(respinfo.getString("respcode"));
respInfo.setRespdes(respinfo.getString("respdes"));
response_head.setRespinfo(respInfo);
response.setResponse_head(response_head);
// 解析response_body
Response_body response_body = new Response_body();
JSONObject responseBody = jsonObject.getJSONObject("response_body");
JSONArray jsonArray = responseBody.getJSONArray("crset");
List<Merchant> merchants = new ArrayList<Merchant>();
for (int i = 0; i < jsonArray.length(); i++) {
// 解析每个merchant
JSONObject item = jsonArray.getJSONObject(i);
Merchant merchant = new Merchant();
merchant.setMerchantname(item.getString("merchantname"));
// 解析每个menu
List<Menu> menus = new ArrayList<Menu>();
JSONArray array = item.getJSONArray("menu");
for (int j = 0; j < array.length(); j++) {
JSONObject menuitem = array.getJSONObject(i);
Menu menu = new Menu();
menu.setMenuid(menuitem.getString("menuid"));
menu.setMenuname(menuitem.getString("menuname"));
menus.add(menu);
}
merchant.setMenu(menus);
merchants.add(merchant);
}
response_body.setCrset(merchants);
response.setResponse_body(response_body);

2 使用google的开源库gson解析,并保存在指定的对象中
Gson gson = new Gson();
Type type = new TypeToken<Response>(){}.getType();
Response response = gson.fromJson(json, type);

Ⅹ android json解析成map格式

先看json数据
之前使用的是gson解析,把 discount 写成Object格式,但是通过解析之后转成String类型,成为

这种明显没有办法解析成map集合
使用原始的Json解析,一层一层的找到这个对象,转成String类型成为

这种情况下就可以了 之后是的解析方法
第一种方法是

第二种是

这样就可以获取到map集合了

热点内容
下载winrar解压缩 发布:2025-07-15 10:59:36 浏览:314
光遇无翼号怎么弄安卓 发布:2025-07-15 10:45:59 浏览:365
什么是法人账号密码 发布:2025-07-15 10:34:59 浏览:876
编程题抽奖 发布:2025-07-15 10:34:00 浏览:629
linux手动编译的内核怎么删 发布:2025-07-15 10:31:56 浏览:96
存储行业发展趋势 发布:2025-07-15 10:25:22 浏览:243
怎么删除配置提示 发布:2025-07-15 10:21:27 浏览:246
java深入学习 发布:2025-07-15 10:13:50 浏览:535
linux应用程序开发pdf 发布:2025-07-15 10:11:37 浏览:912
解压冷知识 发布:2025-07-15 10:11:35 浏览:79