json串的其中一个例子
json串要映射的实体(set、get方法都有)
输出变量为"_id"的语句
额,这个空和你的get Set方法没关系吧?你的代码在实例化对象的时候肯定出错了。
按照你的截图 你的 json串是个 json对象 而不是一个 jsonArray 即 json 数组 所以 应该用 JSON.parseObject 方法
package com.ong.bean;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
public class Test {
static class TestBean{
private String _id;
private String name;
/**
* @return the _id
*/
public String get_id() {
return _id;
}
/**
* @param _id the _id to set
*/
public void set_id(String _id) {
this._id = _id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
}
public static String generateJsonStr(){
String str = "";
str = "{'_id':'qqqqqqqwwwwwww','name':'123123123123123'}";
return str;
}
public static void main(String[] args) {
String str = generateJsonStr();
System.out.println("Original str is "+str);
TestBean testBean = JSON.parseObject(str,TestBean.class);
System.out.println(testBean.get_id());
}
}
运行结果
Original str is {'_id':'qqqqqqqwwwwwww','name':'123123123123123'}
qqqqqqqwwwwwww
ObjectMapper mapper = new ObjectMapper();
BaseHotNews bhn = mapper.readValue(doGet, BaseHotNews.class);
Systemo.out.println(bhn.get_id());
这是使用Jackson解析
不知道你用的什么工具进行json转对象的,把_id 变量换个试下,有可能开发者没考虑你这种命名规则呢
我认为BaseTimeSlot中的sec应该是long型,不是string
为什么要注入到 类对象里面? 直接注入map 或者jsonObject 对象 或者str 然后转 jsonObject 对象不好,然后用jsonObject 转 自定义对象也好,你还可以检查参数是否注入,为什么要直接注入对象? 你用自定义的类 约束太强了,