導航:首頁 > 數據處理 > java如何解析json數據

java如何解析json數據

發布時間:2022-01-23 02:48:55

1. 用java解析json 格式的字元.該如何解析

String temp="{'data':{'a':[{'b1':'bb1','c1':'cc1'},{'b2':'bb2','c2':'cc2'}]}}";
JSONObject jodata =JSONObject.fromObject(temp);
JSONObject joa =JSONObject.fromObject(jodata.get("data").toString());
JSONArray ja=JSONArray.fromObject(joa.get("a"));
for(int i=0;i<ja.size();i++){
JSONObject o=ja.getJSONObject(i);
if(o.get("b1")!=null){
System.out.println(o.get("b1"));
}
if(o.get("c1")!=null){
System.out.println(o.get("c1"));
}
if(o.get("b2")!=null){
System.out.println(o.get("b2"));
}
if(o.get("c2")!=null){
System.out.println(o.get("c2"));
}
}
}
註:要包含兩個jar包ezmorph-1.0.6.jar和json-lib-2.2.2-jdk15.jar,jar包在附件中

2. java怎麼解析我這個json數據

你這個JSON格式,就是數組裡面放數組,所以是,取JSON對象》取JSON數組data》取JSON數組。

importjava.util.ArrayList;
importjava.util.Iterator;
importnet.sf.json.*;
publicclassMainClass{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
JSONObjectjsonObj=JSONObject.fromObject(JsonData.getData());
JSONArrayjsonArr=jsonObj.getJSONArray("data");
Iterator<JSONArray>itr=jsonArr.iterator();
JSONArraytemp;
while(itr.hasNext()){
temp=itr.next();
System.out.println("===========EachJSONArray=========");
for(inti=0;i<temp.size();i++){
System.out.println(temp.get(i));
}
}
}
privatestaticclassJsonData{
privatestaticStringgetData(){
return"{"data":[[5000235,2,3441,8,17,"北京測試","10000101111","","","100001","","2011-09-2317:20:07",18,"vhcDefaultPwd",1,0,"2011-09-2000:00:00",12,0,380,"測試",213,1,0,0,0,0,0,"2012-11-0514:35:23",""],[5000236,27,3442,10,17,"北京測試2","1230000","","","2010920002","111111","2011-09-2317:20:08",18,"vhcDefaultPwd",1,0,"2011-09-2000:00:00",12,0,380,"測試2",213,1,0,0,0,0,0,"2012-11-0514:35:23",""]]}";
}
}
}

3. java 如何解析JSON

一、JSON(JavaScriptObjectNotation)一種簡單的數據格式,比xml更輕巧。Json建構於兩種結構:1、「名稱/值」對的集合(Acollectionofname/valuepairs)。不同的語言中,它被理解為對象(object),紀錄(record),結構(struct),字典(dictionary),哈希表(hashtable),有鍵列表(keyedlist),或者關聯數組(associativearray)。如:{「name」:」jackson」,「age」:100}2、值的有序列表(Anorderedlistofvalues)。在大部分語言中,它被理解為數組(array)如:{「students」:[{「name」:」jackson」,「age」:100},{「name」:」michael」,」age」:51}]}二、java解析JSON步驟A、伺服器端將數據轉換成json字元串首先、伺服器端項目要導入json的jar包和json所依賴的jar包至builtPath路徑下(這些可以到JSON-lib官網下載:http://json-lib.sourceforge.net/)然後將數據轉為json字元串,核心函數是:(Stringkey,Objectvalue){JSONObjectjsonObject=newJSONObject();jsonObject.put(key,value);returnjsonObject.toString();}B、客戶端將json字元串轉換為相應的javaBean1、客戶端獲取json字元串(因為android項目中已經集成了json的jar包所以這里無需導入)publicclassHttpUtil{(StringurlStr){try{//獲取HttpURLConnection連接對象URLurl=newURL(urlStr);HttpURLConnectionhttpConn=(HttpURLConnection)url.openConnection();//設置連接屬性httpConn.setConnectTimeout(3000);httpConn.setDoInput(true);httpConn.setRequestMethod("GET");//獲取相應碼intrespCode=httpConn.getResponseCode();if(respCode==200){returnConvertStream2Json(httpConn.getInputStream());}}catch(MalformedURLExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}return"";}(InputStreaminputStream){StringjsonStr="";//ByteArrayOutputStream相當於內存輸出流ByteArrayOutputStreamout=newByteArrayOutputStream();byte[]buffer=newbyte[1024];intlen=0;//將輸入流轉移到內存輸出流中try{while((len=inputStream.read(buffer,0,buffer.length))!=-1){out.write(buffer,0,len);}//將內存流轉換為字元串jsonStr=newString(out.toByteArray());}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}returnjsonStr;}}2、獲取(StringjsonStr){Personperson=newPerson();try{//將json字元串轉換為json對象JSONObjectjsonObj=newJSONObject(jsonStr);//得到指定jsonkey對象的value對象JSONObjectpersonObj=jsonObj.getJSONObject("person");//獲取之對象的所有屬性person.setId(personObj.getInt("id"));person.setName(personObj.getString("name"));person.setAddress(personObj.getString("address"));}catch(JSONExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}returnperson;}publicstaticListgetPersons(StringjsonStr){Listlist=newArrayList();JSONObjectjsonObj;try{//將json字元串轉換為json對象jsonObj=newJSONObject(jsonStr);//得到指定jsonkey對象的value對象JSONArraypersonList=jsonObj.getJSONArray("persons");//遍歷jsonArrayfor(inti=0;i

4. java獲取json數據方法

你這就是一個Extjs grid 的JsonStore

放到JAVA里的話要先轉成對象

importnet.sf.json.JSONObject;

publicclassTestJson{
staticStringjson_str="{"total":920,"data":[{"ID":"634","Name":"於東"},{"ID":"822","Name":"於禕"},{"ID":"782","Name":"於燕"},{"ID":"636","Name":"於玲"},{"ID":"841","Name":"於浩"},{"ID":"383","Name":"於娟"}]}";
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
JSONObjectjsonObject=JSONObject.fromObject(json_str);
System.out.println(jsonObject.get("total"));
}

}

5. Java解析json數據

一、 JSON (JavaScript Object Notation)一種簡單的數據格式,比xml更輕巧。
Json建構於兩種結構:
1、「名稱/值」對的集合(A collection of name/value pairs)。不同的語言中,它被理解為對象(object),紀錄(record),結構(struct),字典(dictionary),哈希表(hash table),有鍵列表(keyed list),或者關聯數組 (associative array)。 如:
{
「name」:」jackson」,
「age」:100
}

2、值的有序列表(An ordered list of values)。在大部分語言中,它被理解為數組(array)如:
{
「students」:
[
{「name」:」jackson」,「age」:100},
{「name」:」michael」,」age」:51}
]
}
二、java解析JSON步驟
A、伺服器端將數據轉換成json字元串
首先、伺服器端項目要導入json的jar包和json所依賴的jar包至builtPath路徑下(這些可以到JSON-lib官網下載:http://json-lib.sourceforge.net/)

然後將數據轉為json字元串,核心函數是:
public static String createJsonString(String key, Object value)
{
JSONObject jsonObject = new JSONObject();
jsonObject.put(key, value);
return jsonObject.toString();
}
B、客戶端將json字元串轉換為相應的javaBean
1、客戶端獲取json字元串(因為android項目中已經集成了json的jar包所以這里無需導入)
public class HttpUtil
{

public static String getJsonContent(String urlStr)
{
try
{// 獲取HttpURLConnection連接對象
URL url = new URL(urlStr);
HttpURLConnection httpConn = (HttpURLConnection) url
.openConnection();
// 設置連接屬性
httpConn.setConnectTimeout(3000);
httpConn.setDoInput(true);
httpConn.setRequestMethod("GET");
// 獲取相應碼
int respCode = httpConn.getResponseCode();
if (respCode == 200)
{
return ConvertStream2Json(httpConn.getInputStream());
}
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}

private static String ConvertStream2Json(InputStream inputStream)
{
String jsonStr = "";
// ByteArrayOutputStream相當於內存輸出流
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
// 將輸入流轉移到內存輸出流中
try
{
while ((len = inputStream.read(buffer, 0, buffer.length)) != -1)
{
out.write(buffer, 0, len);
}
// 將內存流轉換為字元串
jsonStr = new String(out.toByteArray());
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonStr;
}
}
2、獲取javaBean
public static Person getPerson(String jsonStr)
{
Person person = new Person();
try
{// 將json字元串轉換為json對象
JSONObject jsonObj = new JSONObject(jsonStr);
// 得到指定json key對象的value對象
JSONObject personObj = jsonObj.getJSONObject("person");
// 獲取之對象的所有屬性
person.setId(personObj.getInt("id"));
person.setName(personObj.getString("name"));
person.setAddress(personObj.getString("address"));
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

return person;
}

public static List<Person> getPersons(String jsonStr)
{
List<Person> list = new ArrayList<Person>();

JSONObject jsonObj;
try
{// 將json字元串轉換為json對象
jsonObj = new JSONObject(jsonStr);
// 得到指定json key對象的value對象
JSONArray personList = jsonObj.getJSONArray("persons");
// 遍歷jsonArray
for (int i = 0; i < personList.length(); i++)
{
// 獲取每一個json對象
JSONObject jsonItem = personList.getJSONObject(i);
// 獲取每一個json對象的值
Person person = new Person();
person.setId(jsonItem.getInt("id"));
person.setName(jsonItem.getString("name"));
person.setAddress(jsonItem.getString("address"));
list.add(person);
}
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

return list;
}

6. Java中如何解析Json的字元串得到result、serialNumber的值

import net.sf.json.JSONObject;

public class Test {
public static void main(String[] args) {
String json = "你的json";
JSONObject object = JSONObject.fromObject(json);
JSONObject alipay = object
.getJSONObject("alipay_pass_sync_add_response");
JSONObject biz = alipay.getJSONObject("biz_result");
System.out.println(biz.get("result"));
System.out.println(biz.get("serialNumber"));
System.out.println(alipay.get("error_code"));
System.out.println(alipay.get("success"));
}
}

7. java解析json數據

final String jsonData = "{\"success\":true}";
JSONObject jsonObj = JSONObject.fromObject(jsonData);
boolean success = jsonObj.getBoolean("success");

8. 請大神幫助,java如何解析json數據

java首先導入以下一個包
json-lib-2.3-jdk15.jar
commons-beanutils-1.7.0.jar
commons-httpclient-3.1.jar
commons-lang-2.3.jar
commons-logging-1.0.4.jar
commons-collections-3.1.jar
ezmorph-1.0.3

String dataStr = "{\"resultcode\":\"200\",.......}";
JSONObject json = JSONObject.fromObject(dataStr );
String resultcode = json.get('resultcode');

就是這樣獲取的;

String result = json.get('resultcode');
JSONObject resultJson = JSONObject.fromObject(result );
嵌套的json必須在重新解析

去了解下 JSON的相關api吧

9. java解析json數據成數組

importnet.sf.json.JSONArray;


publicclassTestJson
{
publicstaticvoidmain(String[]args)
{
Stringjson="[{"a":"111","b":"222","c":"333"},{"a":"1000","b":"2000","c":"000"},{"a":"999","b":"300","c":"700"}]";
JSONArrayjsonArr=JSONArray.fromObject(json);
Stringa[]=newString[jsonArr.size()];
Stringb[]=newString[jsonArr.size()];
Stringc[]=newString[jsonArr.size()];
for(inti=0;i<jsonArr.size();i++){
a[i]=jsonArr.getJSONObject(i).getString("a");
b[i]=jsonArr.getJSONObject(i).getString("b");
c[i]=jsonArr.getJSONObject(i).getString("c");
}

for(inti=0;i<c.length;i++){
System.out.print(a[i]+"");
System.out.print(b[i]+"");
System.out.print(c[i]);
System.out.println();
}
}
}

10. java如何讀取json中文件內容

java讀取文件非常簡單的
Filefile=newFile("D:/java"); //給定一個目錄
File[]list=file.listFiles(); //獲取目錄下的所有文件
for(inti=0;i<list.length;i++){
if(list[i].isFile()){ //判斷是否為文件
InputStreamReaderisr=newInputStreamReader(newFileInputStream(list[i]),"UTF-8");//讀取文件,同時指定編碼
StringBuffersb=newStringBuffer();
char[]ch=newchar[128]; //一次讀取128個字元
intlen=0;
while((len=isr.read(ch,0,ch.length))!=-1){
sb.append(ch,0,len);
}
isr.close();
System.out.println(sb); //將讀取完的文件列印出來,你要怎麼處理都可以了
}
}

閱讀全文

與java如何解析json數據相關的資料

熱點內容
釘釘上為什麼會有快遞信息 瀏覽:509
finn是什麼數據類型 瀏覽:716
王者榮耀交易貓怎麼玩 瀏覽:622
創造營3數據統計的網頁是什麼 瀏覽:495
亞馬遜虛擬產品怎麼推廣 瀏覽:295
如何進入研發級程序員 瀏覽:290
寶元加工中心怎麼用子程序 瀏覽:741
個人信息過戶要多少錢 瀏覽:462
律師代理一般提供什麼服務 瀏覽:212
港股交易代碼是什麼時候出來的 瀏覽:128
鴻蒙系統怎麼一鍵關閉後台程序 瀏覽:932
美國市場上雪納瑞多少錢一隻 瀏覽:398
鴻蒙怎麼關閉開啟的程序 瀏覽:168
如何拓展和代理的合作 瀏覽:647
什麼是與市場有關人文活動 瀏覽:787
我是做裝修的如何群發信息給朋友 瀏覽:915
亞馬遜如何改變產品鏈接 瀏覽:108
用什麼技術做音樂 瀏覽:273
普陀代理記賬怎麼辦理 瀏覽:53
程序員是干什麼用的通俗講 瀏覽:498