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

objectmapperjson

發布時間:2021-02-27 14:47:13

1. java ObjectMapper 將對象轉換成json字元串問題

先給你一個正確的方法:

1,把bean裡面的get方法上面的格式去掉

我的代碼如下:

privateTimestamptime;


publicTimestampgetTime(){
returntime;
}

publicvoidsetTime(Timestamptime){
this.time=time;
}

測試方法:

publicstaticvoidmain(String[]args)throwsJsonProcessingException,ParseException{
Timestamptimestamp=newTimestamp(System.currentTimeMillis());
AccountInfoaccountInfo=newAccountInfo();
accountInfo.setTime(timestamp);

ObjectMappermapper=newObjectMapper();
mapper.setDateFormat(newSimpleDateFormat("yyyy-MM-ddhh:mm:ss"));
Strings=mapper.writeValueAsString(accountInfo);
System.out.println(s);

longtime=DateUtils.parseDate("1987-06-0400:00:001","yyyy-MM-ddhh:mm:ss").getTime();
Stringdate=DateUtils.parseDate("1987-06-0400:00:01","yyyy-MM-ddhh:mm:ss").toString();
System.out.println(date);


timestamp=Timestamp.valueOf("1987-06-0400:00:00");
System.out.println(timestamp);
accountInfo=newAccountInfo();
accountInfo.setTime(timestamp);

mapper=newObjectMapper();
mapper.setDateFormat(newSimpleDateFormat("yyyy-MM-ddhh:mm:ss"));
s=mapper.writeValueAsString(accountInfo);
System.out.println(s);
}

輸出結果:

{"openId":null,"token":null,"ip":null,"account":null,"phone":null,"email":null,"platformType":0,"time":"2018-10-1601:27:16"}
ThuJun0400:00:01CDT1987
1987-06-0400:00:00.0
{"openId":null,"token":null,"ip":null,"account":null,"phone":null,"email":null,"platformType":0,"time":"1987-06-0412:00:00"}

不過這里有一個小問題,因為你是使用的yyyy-MM-dd hh:mm:ss 格式,而hh表示按12小時計時,所以1987-06-04 00:00:00會在json中表示為1987-06-04 12:00:00,你可以換成yyyy-MM-dd HH:mm:ss按24小時計進即可。

2. 如何使用ObjectMapper的方式實現Json和bean的自由轉換

第一、將json字元串轉為bean
public class
StudentList { List student; public List getStudent() { return
student; } public void setStudent(List student) { this.student =
student; } @Override public String toString() { return "StudentList
[student=" + student + "]"; } }
public class JsonToJavaBean {
public static void main(String[] args) {
String str="{\"student\":[{\"name\":\"leilei\",\"age\":23},{\"name\":\"leilei02\",\"age\":23}]}";
Student stu = null;
List 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()+" "+s.getAge());
}
}
}

第二、將bean轉為json
public static void main(String[] args) {
ArrayList list=new ArrayList();
Student s1=new Student();
s1.setName("leilei");
s1.setAge(23);
Student s2=new Student();
s2.setName("leilei02");
s2.setAge(23);
list.add(s1);
list.add(s2);

StringWriter str=new StringWriter();

ObjectMapper objectMapper=new ObjectMapper();
try {
objectMapper.writeValue(str, list);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(str);
}

3. java中從伺服器上獲取的json字元串,怎麼存map<integer object>中

如果使用的

net.sf.json

包,可以先用 JSONObject.fromJson(String)轉成jsonobject對象,然後使用toBean方法轉成map,或者用get去一個個版的取。
如果用權的jackson包,
private static ObjectMapper mapper = new ObjectMapper();
mapper.readValue(json, Map.class);

4. objectmapper 有類似於jsonobject.put一樣的api嗎

:可抄能要引幾個包; JSONObject jsonObject = new JSONObject(); jsonObject.put("ret", 0); jsonObject.put("msg", "ok"); jsonObject.put("date", "data"); System.out.println(jsonObject.toString());//你可以列印看一下

5. 如何使用objectmapper的方式實現json和map的自由轉換

第一種:

Java code

public class JsonToJavaBean {
public static void main(String[] args) {
String str="{\"student\":[{\"name\":\"leilei\",\"age\":23},{\"name\":\"leilei02\",\"age\":23}]}";
Student stu = null;
List<Student> 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()+" "+s.getAge());
}
}
}

第二種:

Java code

public static void main(String[] args) {
ArrayList<Student> list=new ArrayList<Student>();
Student s1=new Student();
s1.setName("leilei");
s1.setAge(23);
Student s2=new Student();
s2.setName("leilei02");
s2.setAge(23);
list.add(s1);
list.add(s2);

StringWriter str=new StringWriter();

ObjectMapper objectMapper=new ObjectMapper();
try {
objectMapper.writeValue(str, list);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(str);
}

前幾天剛遇到這個問題,就順便把json完整的學了一遍。
希望能符合您的要求。

6. 如何使用ObjectMapper的方式實現Json和bean的自由轉換

第一種:

Java code

public class JsonToJavaBean {
public static void main(String[] args) {
String str="{\"student\":[{\"name\":\"leilei\",\"age\":23},{\"name\":\"leilei02\",\"age\":23}]}";
Student stu = null;
List<Student> 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()+" "+s.getAge());
}
}
}

第二種:

Java code

public static void main(String[] args) {
ArrayList<Student> list=new ArrayList<Student>();
Student s1=new Student();
s1.setName("leilei");
s1.setAge(23);
Student s2=new Student();
s2.setName("leilei02");
s2.setAge(23);
list.add(s1);
list.add(s2);

StringWriter str=new StringWriter();

ObjectMapper objectMapper=new ObjectMapper();
try {
objectMapper.writeValue(str, list);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(str);
}

前幾天剛遇到這個問題,就順便把json完整的學了一遍。

7. 如何使用ObjectMapper的方式實現Json和bean的自由轉換

public class JsonToJavaBean {
public static void main(String[] args) {
String str="{\"student\":[{\"name\":\"leilei\",\"age\":23},{\"name\":\"leilei02\",\"age\":23}]}";
Student stu = null;
List<Student> 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()+" "+s.getAge());
}
}
}

8. 如何使用ObjectMapper的方式實現Json和bean的自由轉換

網上很流行的一種json和bean之間轉換的方式是 通過JSONObject和JSONArray來讀取json中的數據,然後新建相內應對象並放入其中。也就是容手動擋創建對象。 這種方法不錯,但是最近發現還有一種方法就是用 ObjectMapper.ReadValue的方式。

9. objectmapper 和 jsonparser有什麼區別

區別在於JSONObject是一個{}包裹起來的一個對象(Object), 而JSONArray則是[]包裹起來的一個數組(Array), 說白點就是一個是數組一個是對象或字元串

閱讀全文

與objectmapperjson相關的資料

熱點內容
紅頭文件訂書紅線在哪裡 瀏覽:851
excel怎麼做文件頭 瀏覽:703
為什麼word找不到文件菜單 瀏覽:671
ios15安裝後找不到描述文件 瀏覽:440
學編程怎麼學最有效 瀏覽:873
通過wifi傳文件到ipad 瀏覽:197
js隨機顏色 瀏覽:611
文件快速復制到u盤的軟體 瀏覽:443
如何檢查無線網路驅動是否正常 瀏覽:468
百度網盤來自分享的文件在哪裡 瀏覽:127
如何發視頻到騰訊視頻文件夾里 瀏覽:510
微信打開cad外部文件找不到 瀏覽:882
平板可以打開各種文件後綴的軟體 瀏覽:531
蘋果微信文件怎麼全選 瀏覽:749
手機里之前打開過的文件在哪裡找 瀏覽:685
cad能看word文件嗎 瀏覽:719
12306火車票系統後台資料庫 瀏覽:570
js翻譯德語 瀏覽:33
從哪裡可以下載a股的數據 瀏覽:437
邏輯文件名和物理文件名關系 瀏覽:66

友情鏈接