解决方法:
如果转换的类中有集合,可以先定义Map<String,Class> classMap = new HashMap<String,Class>();
在classMap中put你要转换的类中的集合名,像classMap.put("data",VideoStatusData.class);
然后在toBean()的时候把参数加上,像:VideoNormalData vd1 = (VideoNormalData) JSONObject.toBean(JSONObject.fromObject(str),VideoNormalData.class,classMap);
解析代码:
List<VideoStatusData> list = new LinkedList<VideoStatusData>();
list.add(new VideoStatusData("live","test"));
list.add(new VideoStatusData("live","A361_2"));
VideoNormalData vd = new VideoNormalData("get_stream_status",list);
String json = JSONObject.fromObject(vd).toString();
TcpData tcp = new TcpData("****",3230,json);
String str = tcp.Do();//返回json格式字符串 如下
//{"succ":"0","msg":"stream status result.","data":[{"app":"live","stream":"test","status":"running"},{"app":"live","stream":"A361_2","status":"stoped"}]}
Map<String,Class>();
classMap.put("data",VideoStatusData.class);
VideoNormalData vd1 = (VideoNormalData) JSONObject.toBean(JSONObject.fromObject(str),classMap);
//全部测试代码如下
package api.method;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
import api.basic.TcpData;
import api.data.VideoNormalData;
import api.data.VideoStatusData;
public class VideoStatusRequest {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
List<VideoStatusData> list = new LinkedList<VideoStatusData>();
list.add(new VideoStatusData("live",list);
String json = JSONObject.fromObject(vd).toString();
TcpData tcp = new TcpData("*****",json);
String str = tcp.Do();
Map<String,classMap);
System.out.println(vd1.getData().get(0).getStatus());
}
}
//VideoNormalData 对象代码:
package api.data;
import java.util.List;
public class VideoNormalData {
public VideoNormalData() {
}
public VideoNormalData(String action,List<VideoStatusData> params) {
this.action = action;
this.params = params;
}
private String action;
private String succ;
private String msg;
private List<VideoStatusData> params;
private List<VideoStatusData> data;
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public String getSucc() {
return succ;
}
public void setSucc(String succ) {
this.succ = succ;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public List<VideoStatusData> getParams() {
return params;
}
public void setParams(List<VideoStatusData> params) {
this.params = params;
}
public List<VideoStatusData> getData() {
return data;
}
public void setData(List<VideoStatusData> data) {
this.data = data;
}
}
//VideoStatusData 对象代码
package api.data;
public class VideoStatusData {
public VideoStatusData() {
}
public VideoStatusData(String app,String stream) {
this.app = app;
this.stream = stream;
}
private String app;
private String stream;
private String status;
public String getApp() {
return app;
}
public void setApp(String app) {
this.app = app;
}
public String getStream() {
return stream;
}
public void setStream(String stream) {
this.stream = stream;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
//TcpData 代码
package api.basic; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.InetSocketAddress; import java.net.Socket; public class TcpData { private String hostname; private int port; private String content; public TcpData(String hostname,int port,String content){ this.hostname = hostname; this.port = port; this.content = content; } public String Do() throws Exception { String result=""; PrintWriter out = null; BufferedReader in = null; try { Socket socket = new Socket(); //socket链接指定的主机,超过10秒抛出链接不上异常 socket.connect(new InetSocketAddress(hostname,port),10000); // 得到请求的输出流对象 out = new PrintWriter(socket.getOutputStream()); // 发送请求参数 out.print(content); // flush输出流的缓冲 out.flush(); // 定义 BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF-8")); String line; while ((line = in.readLine()) != null) { result += line; } System.out.println("获取的结果为:"+result); } catch(Exception ex) { System.out.println("发送 POST 请求出现异常!"+ex); ex.printStackTrace(); throw ex; } finally { try{ if(out!=null){ out.close(); } if(in!=null){ in.close(); } } catch(IOException ex){ ex.printStackTrace(); } } return result; } }
原文链接:https://www.f2er.com/json/289820.html