導航:首頁 > 編程語言 > 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相關的資料

熱點內容
cocoapods版本更新 瀏覽:361
國外it大神教你學習如何編程 瀏覽:312
手機存儲應用程序 瀏覽:284
頁面自適應屏幕如何調整代碼數據 瀏覽:681
jsjson工具 瀏覽:299
資料庫中如何備份一張表的數據 瀏覽:739
網路設備能用到什麼 瀏覽:64
暴風轉碼如何添加文件夾 瀏覽:515
延安整合網路營銷有哪些 瀏覽:74
查找word打開過的文件在哪裡 瀏覽:137
b樹java代碼 瀏覽:683
電腦文件存儲 瀏覽:657
蘭州中考徵集志願在哪個網站 瀏覽:215
cs文件上傳下載 瀏覽:244
拷貝文件到根目錄下重命名linux 瀏覽:603
api函數的頭文件 瀏覽:249
華為怎麼綁定迷你編程 瀏覽:215
機構怎麼申請少兒編程考級 瀏覽:495
崑山數控編程哪裡好學 瀏覽:459
jspcfor跳出 瀏覽:65

友情鏈接