Android版fastjson JSON.toJSONString 传入org.json.JSONObject后只打印{}

eqfvzcg8  于 2021-11-27  发布在  Java
关注(0)|答案(0)|浏览(227)
org.json.JSONObject req = new org.json.JSONObject();
req.put("id", 1111);
req.put("name", "name11");
System.out.println(com.alibaba.fastjson.JSON.toJSONString(req)); // 输出"{}"

我看master上的SerializeConfig.java中会对org.json.JSONObject做兼容处理,

fastjson/src/main/java/com/alibaba/fastjson/serializer/JSONObjectCodec.java

Lines 8 to 29 in 8697461

| | publicclassJSONObjectCodecimplementsObjectSerializer { |
| | publicfinalstaticJSONObjectCodec instance =newJSONObjectCodec(); |
| | |
| | publicvoidwrite(JSONSerializerserializer, Objectobject, ObjectfieldName, TypefieldType, intfeatures) |
| | throwsIOException { |
| | SerializeWriter out = serializer.out; |
| | MapSerializer mapSerializer =MapSerializer.instance; |
| | |
| | try { |
| | Field mapField = object.getClass().getDeclaredField("map"); |
| | if (Modifier.isPrivate(mapField.getModifiers())) { |
| | mapField.setAccessible(true); |
| | } |
| | |
| | Object map = mapField.get(object); |
| | mapSerializer.write(serializer, map, fieldName, fieldType, features); |
| | |
| | } catch (Exception e) { |
| | out.writeNull(); |
| | } |
| | } |
| | } |

fastjson/src/main/java/com/alibaba/fastjson/serializer/SerializeConfig.java

Lines 733 to 736 in 8697461

| | if (className.equals("org.json.JSONObject")) { |
| | put(clazz, writer =JSONObjectCodec.instance); |
| | return writer; |
| | } |

为什么 1.1.71-android 没有相关改动,希望此处更新能同步至Android分支上去https://github.com/alibaba/fastjson/blob/1.1.71.android/src/main/java/com/alibaba/fastjson/serializer/SerializeConfig.java

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题