FastJson序列化问题求解【JsonA对象中包含Json对象B,Json对象B转变成String存储】

隔壁老中医 2015-06-29 02:59:59
1、Json代码:
{
"component_name": "基础组件",
"ID":"Base",
"component_description": "This is base component for RichWay programming platform",
"component_category": "Base",
"component_package_name": "base",
"component_path": "/rich/base/web/js",
"component_thumbnail": "",
"config_module": {
"name": "base/admin/base_config",
"package": "base",
"location": "/rich/base/web/js"
}

2、java对象
public class BaseJsonModel {
@JSONField(name="ID")
private String component_id;
private String component_name;
private String component_description;
private String component_category;
private String component_package_name;
private String component_path;
private String component_thumbnail;
private String config_module;
……


4、解释:
1)、Json字符串中的config_module为Json对象,但是存储到数据库中的是一个text,方便页面使用,这样的需求该用fastJson如何解决,有没有遇到的,尝试用过config_module加''单引号,头说不愿意,让解决这个类型的需求
2)、这个还需要从数据看中查出来。
...全文
442 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
广东脸书 2015-08-24
  • 打赏
  • 举报
回复
为什么我parseObject的时候myExtraProcessor中的getExtraType方法没有进来?
隔壁老中医 2015-07-03
  • 打赏
  • 举报
回复
引用 4 楼 tianfang 的回复:
是json解析回java对象的问题吗 序列化的用词不当啊
哈哈,我也发现了,应该是反序列化,混淆了,欢迎前来围观回程问题 http://bbs.csdn.net/topics/391065649
隔壁老中医 2015-07-03
  • 打赏
  • 举报
回复
引用 3 楼 scmod 的回复:
反序列化?你是说再转回json字符串么? 我转出来都是好的啊不是null BaseJsonModel [component_id=Base, component_name=基础组件, component_description=This is base component for RichWay programming platform, component_category=Base, component_package_name=base, component_path=/rich/base/web/js, component_thumbnail=, config_module={"location":"/rich/base/web/js","name":"base/admin/base_config","package":"base"}] 转回来不是null啊... {"ID":"Base","component_category":"Base","component_description":"This is base component for RichWay programming platform","component_name":"基础组件","component_package_name":"base","component_path":"/rich/base/web/js","component_thumbnail":"","config_module":"{\"location\":\"/rich/base/web/js\",\"name\":\"base/admin/base_config\",\"package\":\"base\"}"} 只是他这段还是个string不能当做jsonObject操作, 不过给页面的话对象还是文本不是都是一样的么?传给页面都是个字符串
config_module为json对象,fastJson给实体属性赋值的时候找不到会直接跳过了,我的为null了,后来解决了,方法也贴出来了;你后面说的那个问题就是我现在在纠结的,要把这个json串中String类型的config_module 硬生生的转成json对象返回去,因为页面需要这个类型,能直接解析。 帖子已经发布,欢迎前来围观提提建议,http://bbs.csdn.net/topics/391065649
隔壁老中医 2015-07-03
  • 打赏
  • 举报
回复
后来解决了,重写了一下fastJson的方法 class MyExtraProcessor implements ExtraProcessor, ExtraTypeProvider { public void processExtra(Object object, String key, Object value) { } public Type getExtraType(Object object, String key) { String objNm=object.getClass().getSimpleName().toLowerCase(); if (needAttrStrs.contains(key)||needAttrStrs.contains(objNm+"."+key)) { return String.class; } return null; } }; ExtraProcessor processor = new MyExtraProcessor(); Feature features = Feature.AllowComment; Object baseObj = JSON.parseObject(key, initClass, processor, features); 意思是将特定的属性值转换为string类型,因为json文件中的属性值是json对象的类型,这样Json转对象的时候在实体中会找不到相应的属性,也就为null了 【PS】但是问题又来了,转回来的时候,需要还原,又是一个问题,好烦躁,我新开一个帖子
tianfang 2015-07-01
  • 打赏
  • 举报
回复
是json解析回java对象的问题吗 序列化的用词不当啊
scmod 2015-06-30
  • 打赏
  • 举报
回复
反序列化?你是说再转回json字符串么? 我转出来都是好的啊不是null BaseJsonModel [component_id=Base, component_name=基础组件, component_description=This is base component for RichWay programming platform, component_category=Base, component_package_name=base, component_path=/rich/base/web/js, component_thumbnail=, config_module={"location":"/rich/base/web/js","name":"base/admin/base_config","package":"base"}] 转回来不是null啊... {"ID":"Base","component_category":"Base","component_description":"This is base component for RichWay programming platform","component_name":"基础组件","component_package_name":"base","component_path":"/rich/base/web/js","component_thumbnail":"","config_module":"{\"location\":\"/rich/base/web/js\",\"name\":\"base/admin/base_config\",\"package\":\"base\"}"} 只是他这段还是个string不能当做jsonObject操作, 不过给页面的话对象还是文本不是都是一样的么?传给页面都是个字符串
隔壁老中医 2015-06-29
  • 打赏
  • 举报
回复
但是这个在反序列化的时候为null了,正在看fastJson的wiki,希望有解决方案
scmod 2015-06-29
  • 打赏
  • 举报
回复
这样拿出来不是已经是这样个字符串了? {"location":"/rich/base/web/js","name":"base/admin/base_config","package":"base"}

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧