本文目錄一覽:
- 1、json字符串怎麼轉換為java對象
- 2、怎麼將json對象轉換成一個java對象
- 3、json數組轉java對象怎麼轉?
- 4、Java中JSONObject為什麼要轉換成Java對象來使用
- 5、json格式的字符串轉換為java對象
json字符串怎麼轉換為java對象
您好,鑒於此問題,我將提供您如下的解決方案:
1、首先,加入如下 jar 文件到工程中:
2、編寫 java 代碼,進行json 數據的轉換
3、控制台輸出結果如下所示:
{“age”:90,”houses”:[],”id”:1,”name”:”liNing”}
User [age=90, houses=[], id=1, name=liiNing]
怎麼將json對象轉換成一個java對象
public static void jsonStrToJava(){
//定義兩種不同格式的字符串
String objectStr=”{\”name\”:\”JSON\”,\”age\”:\”24\”,\”address\”:\”北京市西城區\”}”;
String arrayStr=”[{\”name\”:\”JSON\”,\”age\”:\”24\”,\”address\”:\”北京市西城區\”}]”;
//1、使用JSONObject
JSONObject jsonObject=JSONObject.fromObject(objectStr);
Student stu=(Student)JSONObject.toBean(jsonObject, Student.class);
//2、使用JSONArray
JSONArray jsonArray=JSONArray.fromObject(arrayStr);
//獲得jsonArray的第一個元素
Object o=jsonArray.get(0);
JSONObject jsonObject2=JSONObject.fromObject(o);
Student stu2=(Student)JSONObject.toBean(jsonObject2, Student.class);
System.out.println(“stu:”+stu);
System.out.println(“stu2:”+stu2);
}
json數組轉java對象怎麼轉?
1、需要json的相關jar包
2、設置json格式字符串:
String str=”{\”student\”:[{\”name\”:\”leilei\”,\”age\”:23},{\”name\”:\”leilei02\”,\”age\”:23}]}”;
3、建立相應的類:
public class StudentList {
ListStudent student;
public ListStudent getStudent() {
return student;
}
public void setStudent(ListStudent student) {
this.student = student;
}
}
public class Student {
private String name;
private int age;
//private StudentClass studentClass;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
4、測試程序
import java.util.List;
import org.codehaus.jackson.map.ObjectMapper;
public class JsonToJavaBean {
public static void main(String[] args) {
String str=”{\”student\”:[{\”name\”:\”leilei\”,\”age\”:23},{\”name\”:\”leilei02\”,\”age\”:23}]}”;
Student stu = null;
ListStudent list = null;
try {
ObjectMapper objectMapper=new ObjectMapper();
StudentList studentList=objectMapper.readValue(str, StudentList.class);
list=studentList.getStudent();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(Student s:list){
System.out.println(s.getName());
}
}
Java中JSONObject為什麼要轉換成Java對象來使用
JSONObject在JAVA中其實就相當於Map, 可以通過key取到值;
而JAVA對象,一般是JAVA中的model或者domain 模型對象, 這種對象代表的是一個業務的模型,是有明確的意義的, 包括裡面的屬性類型都是明確定義;
轉換成對象之後,操作其中的值就可以用getter,setter方法明確指定, 一般其他人使用的時候也能知道裡面有什麼,而如果只有一個JSONObject, 使用的時候除了先在控制台輸入裡面的內容是不知道裡面有什麼的, 並不適合在JAVA方法中的數據傳遞使用
json格式的字符串轉換為java對象
先來解釋反斜杠幹嘛用的:
你既然提到了是json字符串,對,請注意字符串三個字。什麼是字符串?兩個引號之間的就是一個字符串比如:”abcdefg”這就是字符串.
假如我有個json對象:
{
“message” : “success”
}
那麼問題來了,怎麼把它放到一個String字符串對象里呢?
那麼我們都會這麼做:
String json = “{“message”:”success”}”;
對吧?但是這麼做對嗎?前面說過了,字符串是引號開始,引號結尾的,再看一眼這麼做對嗎?
所以我們需要轉義字符去將引號放入字符串當中,而不是字符串定義的功能。“ \” ”就是表示將雙引號放入字符串中。
2. json字符串轉java對象
下面以ali的fastjson為例,講解json字符串轉json對象:
String jsonStr = “{\”message\” : \”success\”}”;
JSONObject jsonObject = JSON.parseObject(jsonStr);
String message = jsonObject.getString(“message”);
System.out.println(message);
假如你有想要轉換成的bean :
public class Info {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
就可以這樣:
String jsonStr = “{\”message\” : \”success\”}”;
Info info = JSON.parseObject(jsonStr, Info.class);
System.out.println(info.getMessage());
這裡的fastjson只是處理json的框架中的一個,此外還有jackson、Gson等等,可以根據自己的需求選擇一個就可以了。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/192079.html