本文目錄一覽:
- 1、請問Java中json是什麼?
- 2、java 如何解析JSON
- 3、java開發,json是幹什麼的
- 4、java怎麼讀取json格式的數據
- 5、Java解析json數據
- 6、java中把json怎麼轉換成數組?
請問Java中json是什麼?
一 簡介:
JSON(JavaScript對象符號)是一種輕量級的數據交換格式。這是很容易為人類所讀取和寫入。這是易於機器解析和生成。它是基於JavaScript編程語言的一個子集 , 標準ECMA-262第三版- 1999年12月。JSON是一個完全獨立於語言的文本格式,但使用C家族的語言,包括C,C + +,C#,Java中的JavaScript,Perl的,Python中,和許多其他程序員所熟悉的約定。這些特性使JSON成為理想的數據交換語言。他和map很類似,都是以
鍵/值 對存放的。
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官網下載:)然後將數據轉為json字符串,核心函數是:publicstaticStringcreateJsonString(Stringkey,Objectvalue){JSONObjectjsonObject=newJSONObject();jsonObject.put(key,value);returnjsonObject.toString();}B、客戶端將json字符串轉換為相應的javaBean1、客戶端獲取json字符串(因為android項目中已經集成了json的jar包所以這裡無需導入)publicclassHttpUtil{publicstaticStringgetJsonContent(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””;}privatestaticStringConvertStream2Json(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、獲取javaBeanpublicstaticPersongetPerson(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
java開發,json是幹什麼的
json其實就是封裝了一種數據格式,它使用了自己定義的標準。主要用來在服務器和客戶端的瀏覽器進行數據交換。因為我們常用的表單形式提交數據,有諸多的不便,json解決了一些問題。學習Java開發推薦千鋒教育,千鋒教育利用技術優勢精心打造了AI教輔系統,依託技術領域熱門的人工智能技術,科技輔學,有力護航學員成長。
java開發前景好,很多軟件的開發都離不開Java,因此其程序員的數量最多。據官方數據統計,在全球編程語言工程師的數量上,Java語言以900萬的程序員數量位居首位。Java在我們的生活中無處不在。只要我們能接觸到互聯網,我們就不能沒有Java。目前,世界上有數十億設備正在運行Java。從互聯網電子商務到金融行業的服務器應用,從APP到企事業單位的OA系統,從大數據到桌面應用等,Java廣泛應用於各個領域。
想要了解更多關於java開發的相關信息,推薦諮詢千鋒教育。千鋒企合作部整合大量企業客戶資源,緊抓當下企業需求,將技術和項目完美結合千鋒課程體系,力求培養更多優質人才服務企業,不斷提升學員競爭力,鏈接企業用人標準的培訓課程及實戰項目,讓企業招聘用人的技術要求與千鋒學員的技術充分對接。近年來不斷引進阿里釘釘小程序技術、紅帽認證、騰訊雲、亞馬遜等,通過與企業的深度融合實現千鋒教研和就業服務的迭代升級,專業性值得信賴。
java怎麼讀取json格式的數據
java可以使用JSONObject和JSONArray來操作json對象和json數組,具體用法如下
1:java對象與json串轉換:
java對象—json串:
JSONObject JSONStr = JSONObject.fromObject(object);
String str = JSONStr.toString();
json串—java對象:
JSONObject jsonObject = JSONObject.fromObject( jsonString );
Object pojo = JSONObject.toBean(jsonObject,pojoCalss);
2:java數組對象與json串轉換:
java數組—json串:
JSONArray arrayStr = JSONArray.fromObject(List?);
String str = arrayStr.toString();
json串—java數組:
JSONArray array = JSONArray.fromObject(str);
List? list = JSONArray.toList(array, ?.class);
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官網下載:)
然後將數據轉為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 ListPerson getPersons(String jsonStr)
{
ListPerson list = new ArrayListPerson();
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;
}
java中把json怎麼轉換成數組?
使用原生的解析:
String json = “…”;
//遍曆數組裡的值,得到每個獨立的對象,然後獲取對應的值設置到聲明好的對象中,最終創建對象完成後添加到集合中,如我自己代碼里的片段:
for (int j = 0; j array.length(); j++) {
obj = array.getJSONObject(j);
Data data = new Data();
mDataList.add(data);
}
數組聲明
在數組的聲明格式里,「數據類型」是聲明數組元素的數據類型,可以是java語言中任意的數據類型,包括簡單類型和結構類型。「數組名」是用來統一這些相同數據類型的名稱,其命名規則和變量的命名規則相同。
數組聲明之後,接下來便是要分配數組所需要的內存,這時必須用運算符new,其中「個數」是告訴編譯器,所聲明的數組要存放多少個元素,所以new運算符是通知編譯器根據括號里的個數,在內存中分配一塊空間供該數組使用。利用new運算符為數組元素分配內存空間的方式稱為動態分配方式。
以上內容參考:百度百科-數組
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/248642.html