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

javajsonjackson

發布時間:2022-06-26 18:24:01

A. java jackson 如何解析json

String json = "{\"success\":true,\"A\":{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"},"+
"\"B\":{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}}";
try {
ObjectMapper objectMapper=new ObjectMapper();
Map<String, Map<String, Object>> maps = objectMapper.readValue(json, Map.class);
System.out.println(maps.size());
Set<String> key = maps.keySet();
Iterator<String> iter = key.iterator();
while (iter.hasNext()) {
String field = iter.next();
System.out.println(field + ":" + maps.get(field));
}
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

B. jackson解析java對象為json字元串時,怎麼用註解的方式動態過濾屬性

下載這幾個包,導入項目:

  1. JSONObject obj= new JSONOjbect().fromObject(test);

  2. JSONObject json= new JSONOjbect().fromObject(obj.getObject("xxx"));

  3. System.out.println(json.getString("A"));

  4. System.out.println(json.getString("B"));

C. jackson和json的區別

ackson可以輕松的將Java對象轉換成json對象和xml文檔,同樣也可以將json、xml轉換成Java對象。
前面有介紹過json-lib這個框架,在線博文:http://www.cnblogs.com/hoojo/archive/2011/04/21/2023805.html
相比json-lib框架,Jackson所依賴的jar包較少,簡單易用並且性能也要相對高些。
而且Jackson社區相對比較活躍,更新速度也比較快。

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

E. java中怎樣使用jackson

//還需要導包的,你可以把下面直接加到pom.xml文件,直接導入的,或是網上下載
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.3</version>
</dependency>
<dependencies>
java轉換格式還有另外一個,希望可以幫助到你
ObjectMapper om = new ObjectMapper();
String jsonStr = om.writeValueAsString(list);

F. java怎麼將json轉換成對象

可以使用jackson的 new ObjectMapper().readValue(String, bean.class);
或者使用 JSONObject.toBean(String,bean.class);
都可以實現json轉化為bean
而 jackson 或者 JSONObject 的jar包網上均有下載,把下載的jar導入你的項目中即可。

G. jackson解析java對象為json字元串時,怎麼用註解的方式動態過濾屬性

下載這幾個包,導入項目, JSONObject obj= new JSONOjbect().fromObject(test); JSONObject json= new JSONOjbect().fromObject(obj.getObject("xxx")); System.out.println(json.getString("A")); System.out.println(json.getString("B"));

H. 關於Java 中使用Jackson轉換json的問題

繼承C,寫一個類吧 。。。。。

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

閱讀全文

與javajsonjackson相關的資料

熱點內容
買男裝用什麼app好 瀏覽:851
文件夾合並了怎麼拆開 瀏覽:256
波段副圖源碼無未來函數 瀏覽:84
livecn伺服器地址 瀏覽:255
程序員這個工作真的很吃香嗎 瀏覽:842
程序員和數學分析師待遇 瀏覽:676
壓縮氣彈簧怎麼拆 瀏覽:319
華為公有雲伺服器添加虛擬ip 瀏覽:207
程序員和運營哪個累 瀏覽:22
抖音安卓信息提示音怎麼設置 瀏覽:454
光速虛擬機的共享文件夾 瀏覽:246
程序員培訓機構發的朋友圈真實性 瀏覽:742
天乾地支簡單演算法 瀏覽:299
下載個壓縮文件 瀏覽:298
普通人電腦關機vs程序員關機 瀏覽:628
米酷建站源碼 瀏覽:115
氫氣app怎麼搜搭配 瀏覽:619
pdf綠盟 瀏覽:505
固態硬碟編譯器重建 瀏覽:391
怎樣編輯硬碟文件夾 瀏覽:660