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

androidjson解析对象

发布时间: 2022-06-14 13:15:44

㈠ android 手机怎样解析json数据

可以用一些开源的java库,就可以把这些json数据直接变为Java对象及数组了,然后用Java处理起来很方便。
常用的处理json的库有gson和json-lib,你网络一下可以下到,网上也有例子

㈡ android怎么解析json文件

你好,我试过了,这样能取到你要的结果:

用的gson-2.2.4.jar包,你应该有吧,没有网络搜下去下个就好了。

importcom.google.gson.JsonArray;
importcom.google.gson.JsonObject;
importcom.google.gson.JsonParser;
publicclassMyTest{
publicstaticvoidmain(String[]args){
Stringjson="{'resultcode':'200','reason':'ReturnSuccessd!','result':{'data':[{'MCC':'460','MNC':'1','LNG':'120.721423','LAT':'31.29854','O_LNG':'120.72577772352','O_LAT':'31.296529947917','PRECISION':'1101','ADDRESS':'江苏省苏州市吴中区金鸡湖大道368号'}]}}";
JsonParserjsonParser=newJsonParser();
JsonObjectjsonObj=jsonParser.parse(json).getAsJsonObject();
JsonObjectresult=jsonObj.get("result").getAsJsonObject();
JsonArraydata=result.get("data").getAsJsonArray();
StringO_LNG=data.get(0).getAsJsonObject().get("O_LNG").getAsString();
StringO_LAT=data.get(0).getAsJsonObject().get("O_LAT").getAsString();
StringADDRESS=data.get(0).getAsJsonObject().get("ADDRESS").getAsString();
System.out.println(O_LNG);
System.out.println(O_LAT);
System.out.println(ADDRESS);
}
}

㈢ Android 解析json问题

///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();
}

㈣ 安卓中返回的json数据怎么解析

WebService返回的Json字符串或者是XML文件,看你要怎么样用,Android中有解析Json的对象如JSONObject,JsonArray等,着键你的WEBService要配置好。

㈤ 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转换成对象

android中json转换成List<Map> Java代码 package cn.anycall; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class Test { /** * 将json 数组转换为Map 对象 * @param jsonString * @return */ public static Map<String, Object> getMap(String jsonString) { JSONObject jsonObject; try { jsonObject = new JSONObject(jsonString); @SuppressWarnings("unchecked") Iterator<String> keyIter = jsonObject.keys(); String key; Object value; Map<String, Object> valueMap = new HashMap<String, Object>(); while (keyIter.hasNext()) { key = (String) keyIter.next(); value = jsonObject.get(key); valueMap.put(key, value); } return valueMap; } catch (JSONException e) { e.printStackTrace(); } return null; } /** * 把json 转换为ArrayList 形式 * @return */ public static List<Map<String, Object>> getList(String jsonString) { List<Map<String, Object>> list = null; try { JSONArray jsonArray = new JSONArray(jsonString); JSONObject jsonObject; list = new ArrayList<Map<String, Object>>(); for (int i = 0; i < jsonArray.length(); i++) { jsonObject = jsonArray.getJSONObject(i); list.add(getMap(jsonObject.toString())); } } catch (Exception e) { e.printStackTrace(); } return list; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String temp = "[{\"aa\":\"1\",\"bb\":\"2\"},{\"aa\":\"3\",\"bb\":\"4\"},{\"aa\":\"5\",\"bb\":\"6\"}]"; List<Map<String, Object>> lm = Test.getList(temp); for(int i=0;i<lm.size();i++){ System.out.println(lm.get(i).get("aa")); System.out.println(lm.get(i).get("bb")); } } }

㈦ 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文件解析为类对象 出现以下错误,求解答。

用Gson解析json数据是可以的, 如果你非要把解析对象写成类嵌套的形式,就必须要参考gson的用户指南.这是截图:

:最好的办法是不要用类嵌套. 直接写两个类接即可


public class LoginData {
public Data Data;
public String Detail;
public string Return;


}
public static class Data {

public String name;
private String token;
public int uid;

}

㈨ android 解析json用那个包里面的方法比较好呢

android 解析json还有用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对象或数组
这段字符串中以]结尾为数组
JSonArray array = new JSonArray
遍历这个数组获得对应的对象
for(int i = 0 ; i < array.length() ;i++){
JSONObject obj = array.getJSONObject(i);

}

热点内容
安卓手游脚本语言 发布:2025-05-17 19:53:07 浏览:21
找圈算法 发布:2025-05-17 19:49:19 浏览:410
数据库的存取方法 发布:2025-05-17 19:48:36 浏览:125
androidapp测试 发布:2025-05-17 19:48:19 浏览:389
如何修改iphone密码修改 发布:2025-05-17 19:47:31 浏览:509
发现了致富密码是什么意思 发布:2025-05-17 19:45:42 浏览:416
耐存储的纸 发布:2025-05-17 19:43:35 浏览:931
java什么是栈 发布:2025-05-17 19:28:58 浏览:499
开拓者交易云服务器配置 发布:2025-05-17 19:19:47 浏览:12
编程2e8 发布:2025-05-17 19:19:04 浏览:420