導航:首頁 > 編程語言 > josnjava

josnjava

發布時間:2022-09-03 22:22:50

A. java如何返回json格式

處理基本的java對象使用JSONObject類,用法大體如下:

public void testMap(){

Map<String,Object> map = new HashMap<String,Object>();

map.put("name", "qiu");

map.put("password", "123");

map.put("address", "china");

User user = new User();

user.setUserName("qiuqiu");

user.setPassword("123456");

user.getTels().add("1234444556677");

user.getTels().add("6893493458585");

map.put("user", user);

JSONObject json = new JSONObject(map);

System.out.println(json.toString());

}

B. 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

C. java開發,json是干什麼的

json其實就是封裝了一種數據格式,它使用了自己定義的標准。主要用來在伺服器和客戶端的瀏覽器進行數據交換。因為我們常用的表單形式提交數據,有諸多的不便,json解決了一些問題。

D. Java實現JSON多層遍歷

JSONObject jsonObject = new JSONObject(s);
然後用Iterator迭代器遍歷取值,建議用反射機制解析到封裝好的對象中
JSONObject jsonObject = new JSONObject(jsonString);
Iterator iterator = jsonObject.keys();while(iterator.hasNext()){
key = (String) iterator.next();
value = jsonObject.getString(key);
}

E. 請問Java中json是什麼

一 簡介:
JSON(JavaScript對象符號)是一種輕量級的數據交換格式。這是很容易為人類所讀取和寫入。這是易於機器解析和生成。它是基於JavaScript編程語言的一個子集 , 標准ECMA-262第三版- 1999年12月。JSON是一個完全獨立於語言的文本格式,但使用C家族的語言,包括C,C + +,C#,Java中的JavaScript,Perl的,Python中,和許多其他程序員所熟悉的約定。這些特性使JSON成為理想的數據交換語言。他和map很類似,都是以
鍵/值 對存放的。

F. 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;
}

G. 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);

H. java 中如何引用json文件

引用json文件?這個說法夠奇怪的。
JSON格式對象的操作可以藉助於第三方包JSON-lib(需要其他依賴包)或者org.json(不需要其他依賴包)來操作。
你所說的引用JSON文件,是對json格式的文件進行操作么?如果是那樣的話可以利用IO流,讀取文件中的字元串,再利用JSON-lib或者org.json進行處理。

閱讀全文

與josnjava相關的資料

熱點內容
怎麼把安卓視頻傳到蘋果上面 瀏覽:79
手機拍鬼片用什麼app 瀏覽:640
爬山虎app是干什麼用的 瀏覽:505
有哪些寫給程序員的歌 瀏覽:49
成都市命令 瀏覽:993
建立系列文件夾 瀏覽:983
蘋果開機白屏帶文件夾問號 瀏覽:733
體驗服為什麼伺服器會關閉 瀏覽:41
酒店命令 瀏覽:750
中走絲線切割編程視頻 瀏覽:80
衣服壓縮袋手泵原理 瀏覽:714
通達信編程書籍 瀏覽:981
車用壓縮天然氣瓶閥 瀏覽:971
鞋的程序員 瀏覽:259
車的壓縮比是什麼意思 瀏覽:202
網站源碼怎麼傳到文件夾 瀏覽:914
海南壓縮機在哪裡 瀏覽:491
電腦文件夾清晰的文件結構 瀏覽:839
如何把蘋果手機的app轉到安卓 瀏覽:305
java同步並發 瀏覽:249