我正在构建一个使用socket.IO与服务器通信的Android应用程序。在套接字上,我收到以下消息:
{"id":"presenterResponse","response":"accepted","sdpAnswer":"v=0\r\no=- 3689503004 3689503004 IN IP4 0.0.0.0\r\ns=Kurento Media Server\r\nc=IN IP4 0.0.0.0\r\nt=0 0\r\na=msid-semantic: WMS ARDAMS\r\na=group:BUNDLE audio video\r\nm=audio 1 UDP/TLS/RTP/SAVPF 111 0\r\na=extmap:3 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time\r\na=mid:audio\r\na=rtcp:9 IN IP4 0.0.0.0\r\na=rtpmap:111 opus/48000/2\r\na=rtpmap:0 PCMU/8000\r\na=setup:active\r\na=sendrecv\r\na=rtcp-mux\r\na=fmtp:111 minptime=10; useinbandfec=1\r\na=maxptime:60\r\na=ssrc:3140217435 cname:user2918705367@host-8692fd1\r\na=ice-ufrag:aYIP\r\na=ice-pwd:sKftuHNg7pyX0FocNO0qh/\r\na=fingerprint:sha-256 33:B4:A7:4B:89:64:D2:54:AF:6B:FE:D4:5D:EF:4E:D6:AB:6F:11:B1:E7:31:87:D6:0D:22:3F:53:83:08:B7:73\r\nm=video 1 UDP/TLS/RTP/SAVPF 100\r\na=extmap:3 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time\r\na=mid:video\r\na=rtcp:9 IN IP4 0.0.0.0\r\na=rtpmap:100 VP8/90000\r\na=rtcp-fb:100 ccm fir\r\na=rtcp-fb:100 nack\r\na=rtcp-fb:100 nack pli\r\na=rtcp-fb:100 goog-remb\r\na=setup:active\r\na=sendrecv\r\na=rtcp-mux\r\na=ssrc:2322920152 cname:user2918705367@host-8692fd1\r\na=ice-ufrag:aYIP\r\na=ice-pwd:sKftuHNg7pyX0FocNO0qh/\r\na=fingerprint:sha-256 33:B4:A7:4B:89:64:D2:54:AF:6B:FE:D4:5D:EF:4E:D6:AB:6F:11:B1:E7:31:87:D6:0D:22:3F:53:83:08:B7:73\r\nm=application 0 DTLS/SCTP 5000\r\na=inactive\r\na=mid:data\r\na=ice-ufrag:igJW\r\na=ice-pwd:skfFXPCVLtGwABgXg9xsMO\r\na=fingerprint:sha-256 33:B4:A7:4B:89:64:D2:54:AF:6B:FE:D4:5D:EF:4E:D6:AB:6F:11:B1:E7:31:87:D6:0D:22:3F:53:83:08:B7:73\r\n"}
我现在要做的是将此对象映射到以下POJO类:
import org.webrtc.SessionDescription;
public class User {
private String id;
private String response;
private String name;
private String room;
private String email;
private String sdpOffer;
private SessionDescription sdpAnswer;
private String userType;
public User(){};
public SessionDescription getSdpAnswer() {
return sdpAnswer;
}
public void setSdpAnswer(SessionDescription sdpAnswer) {
this.sdpAnswer = sdpAnswer;
}
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserType() {
return userType;
}
public void setUserType(String userType) {
this.userType = userType;
}
public String getSdpOffer() {
return sdpOffer;
}
public void setSdpOffer(String sdpOffer) {
this.sdpOffer = sdpOffer;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRoom() {
return room;
}
public void setRoom(String room) {
this.room = room;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
到目前为止,我已尝试在JSONObject JSONObject j = new JSONObject(message);
中转换消息,并从JSONObject填充我自己的对象,但它没有工作。
如何在User类上映射字符串?
编辑:
消息来自Node.js服务器上的socket.IO以这种方式:
在节点服务器中,我按send.("new message",JSON.stringfy("sdpAnswer":sdpAnswer));
发送消息
在Android客户端上,我读它:
LoginActivity.mSocket.on("new message", new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.i( TAG, "message back:received ");
JSONObject j = new JSONObject(args[0]); // this throws an error
Object = new JSONObject(args[0].toString());
Log.i(TAG, "The received string is " + obj.toString()); //this throws java.lang.error can not convert from string to json object
}
)};
答案 0 :(得分:0)
使用 Gson
String jsonString = "{jsonstringhere}";
User user= new Gson().fromJson(jsonString, User.class);
答案 1 :(得分:0)
使用Gson库。
向build.gradle
文件添加依赖项。
compile 'com.google.code.gson:gson:2.8.0'
将json解析为object:
Gson gson = new Gson();
User user= gson.fromJson(message, User.class);