json巢狀獲取

2021-09-24 05:22:43 字數 2920 閱讀 5018

目前思路

獲取string

string轉化為json

json在變為string

假如json為}

**沒寫全,只是個參考,主要是想記錄一下httpclient一般使用情況,外加json轉變問題

不過現在不怎麼用,一般用okhttp,但我是小白,對於一些服務器官網上用的這個httpclient,本來想用okhttp連線,但總是報錯引數錯誤,就沒有用okhttp

//post       

//獲取httpclient物件

//設定url

// 設定請求的header

httppost.addheader("關鍵字", "值");

// 設定請求的引數

listnvps = new arraylist();

nvps.add(new basicnamevaluepair("關鍵字", "值");

basicnamevaluepair("關鍵字", "值");

/*依賴com.gexin.fastjson.json;這裡不用這方法,換一種,依賴net.sf.json.jsonobject

string code = json.parseobject(responseentity).getstring("code");

string info = json.parseobject(responseentity).getstring("info");

system.out.println(responseentity);

*/jsonobject jsonobject = jsonobject.fromobject(responseentity);

string info = jsonobject.getstring("關鍵字");

string code = jsonobject.getstring("關鍵字");

jsonobject = jsonobject.fromobject(info);

string token =jsonobject.getstring("token");

" "+info+" "+token+" "+responseentity);

//判斷是否傳送成功,傳送成功輸出

/*if (code=="200") */

//get

//獲取httpclient物件

//設定引數,假如為pairs

...//還可以通過構造buider

//執行

//得到實體,這裡簡單些,不判斷是否成功

string result = entitytostring(reponse.getentity);

okhttp連線

//post方法

//拿到okhttpclient物件

//建立requesbody

requestbody requestbody = requestbody.create(mediatype,json);

//構造request

request request = new request.builder()

.url(....)

.addheader(....)

.post(requestbody)

.build();

//執行call

client.newcall(request).enqueue(new callback()

@override

public void onresponse(call call, response response) throws ioexception

});//get方法

//拿到okhttpclient物件

//構造request

request request = new request.builder()

.get()

.url(....)

.addheader(....)

.build();

//執行call

client.newcall(request).enqueue(new callback()

@override

public void onresponse(call call, response response) throws ioexception

});

感覺還是okhttp好用一些

json巢狀json如何解析

json格式如下 使用google的gson包處理,非常簡單 發現這個問題其實挺簡單的。我的解決方法如下 先定義三個類分別為 biz,bizs 和 data,用於對應你的 json 串中的從裡到外的三個部分。biz 類中包含 id,name,add 三個屬性,bizs 中包含壹個 biz 型別的陣列...

關於迴圈巢狀拼接json

因為工作原因,用到了vue的元件,要模仿省市聯動做乙個型別的聯動,所以需要乙個下面這樣子的json資料 label 二級分類1 1 value 11 label 二級分類1 2 value 12 label 二級分類1 3 value 13 label 一級分類1 value 1 label 二級分...

spark解析巢狀JSON陣列

資料是帶有時間戳的json陣列 資料格式 1610352196000 data data 讀取 val tmp sc.textfile in test json.log tmp.foreach println 列印資料 json陣列可根據 n timeserver n timeserver n ti...