當前位置:首頁 » 安卓系統 » 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集合了

熱點內容
硬碟緩存什麼用 發布:2025-05-19 22:09:41 瀏覽:11
蘋果筆記本配置好的有哪些 發布:2025-05-19 22:08:57 瀏覽:14
oracle存儲過程中批量修改表結構 發布:2025-05-19 22:02:22 瀏覽:520
php支付寶sdk 發布:2025-05-19 22:01:06 瀏覽:602
雲掃墓源碼 發布:2025-05-19 22:00:32 瀏覽:593
executeupdatesql 發布:2025-05-19 21:58:36 瀏覽:217
中國電信如何轉人工密碼是多少 發布:2025-05-19 21:44:54 瀏覽:209
求階乘的c語言 發布:2025-05-19 21:15:20 瀏覽:965
話嘮安卓哪裡下載 發布:2025-05-19 20:27:04 瀏覽:166
瘋狂android講義光碟 發布:2025-05-19 20:12:31 瀏覽:153