简单object转json
public void onNext ( Object o) {
JSONObject json = JSONObject . parseObject ( o. toString ( ) ) ;
}
复杂(对象内包对象)json转为Java对象
{
"errno": 0,
"error": "succ",
"data": {
"create_time": "2021-07-05 15:08:37",
"online": false,
"id": "742851839",
"observe_status": false,
"title": "861854049739673"
}
}
Msg
public class Msg {
String errno;
String error;
public Msg ( String errno, String error) {
this . errno = errno;
this . error = error;
}
public String getErrno ( ) { return errno; }
public void setErrno ( String errno) { this . errno = errno; }
public String getError ( ) { return error; }
public void setError ( String error) { this . error = error; }
}
ItemData
package com. shylFm. web. controller. httpConnection ;
public class ItemData {
String create_time;
String online;
String id;
String observe_status;
String title;
public String getCreate_time ( ) { return create_time; }
public void setCreate_time ( String create_time) { this . create_time = create_time; }
public String getOnline ( ) { return online; }
public void setOnline ( String online) { this . online = online; }
public String getId ( ) { return id; }
public void setId ( String id) { this . id = id; }
public String getObserve_status ( ) { return observe_status; }
public void setObserve_status ( String observe_status) { this . observe_status = observe_status; }
public String getTitle ( ) { return title; }
public void setTitle ( String title) { this . title = title; }
}
All
public class All extends Msg {
private ItemData data;
public All ( String errno, String error, ItemData data) {
super ( errno, error) ;
this . data = data;
}
public ItemData getData ( ) { return data; }
public void setData ( ItemData data) { this . data = data; }
}
复杂(对象内包对象)json转为Java对象调用实例
import com. alibaba. fastjson. JSONObject ;
public class test {
public static void main ( String [ ] args) {
String str = "{\"errno\":0,\"error\":\"succ\",\"data\":{\"create_time\":\"2021-07-05 15:08:37\",\"online\":false,\"id\":\"742851839\",\"observe_status\":false,\"title\":\"861854049739673\"}}" ;
All all = JSONObject . parseObject ( str, All . class ) ;
System . out. println ( all. getData ( ) . getId ( ) ) ;
}
}