@Test public void testFromObjectToJSONString() throws Exception { TestObj obj = new TestObj(); obj.a = 1; assertEquals(WXJsonUtils.fromObjectToJSONString(obj),"{\"a\":1}"); assertEquals(WXJsonUtils.fromObjectToJSONString(null),"null"); assertEquals(WXJsonUtils.fromObjectToJSONString(new Object()),"{}"); Map<String,Object> data = new HashMap<>(); data.put("a",536649655068L); BigDecimal d = new BigDecimal(536649655068L); data.put("d",d); data.put("b",new Long(536649655068L)); Object resp = JSON.parse("[{\"offerId\":536449659068,\"subject\":\"jv2测试offer\",\"offerUnit\":\"千克\",\"price\":{\"price\":234.0},\"imageList\":[\"http: data.put("c",resp); String result = WXJsonUtils.fromObjectToJSONString(data); System.out.println(result); }
public @NonNull static String fromObjectToJSONString(Object obj, boolean WriteNonStringKeyAsString){ try { if(WriteNonStringKeyAsString) { return JSON.toJSONString(obj, SerializerFeature.WriteNonStringKeyAsString); }else { return JSON.toJSONString(obj); } }catch(Exception e){ if(WXEnvironment.isApkDebugable()){ throw new WXRuntimeException("fromObjectToJSONString parse error!"); } WXLogUtils.e("fromObjectToJSONString error:", e); return "{}"; } }
WXJsonUtils { public @NonNull static String fromObjectToJSONString(Object obj, boolean WriteNonStringKeyAsString){ try { if(WriteNonStringKeyAsString) { return JSON.toJSONString(obj, SerializerFeature.WriteNonStringKeyAsString); }else { return JSON.toJSONString(obj); } }catch(Exception e){ if(WXEnvironment.isApkDebugable()){ throw new WXRuntimeException("fromObjectToJSONString parse error!"); } WXLogUtils.e("fromObjectToJSONString error:", e); return "{}"; } } }
WXJsonUtils { public @NonNull static String fromObjectToJSONString(Object obj, boolean WriteNonStringKeyAsString){ try { if(WriteNonStringKeyAsString) { return JSON.toJSONString(obj, SerializerFeature.WriteNonStringKeyAsString); }else { return JSON.toJSONString(obj); } }catch(Exception e){ if(WXEnvironment.isApkDebugable()){ throw new WXRuntimeException("fromObjectToJSONString parse error!"); } WXLogUtils.e("fromObjectToJSONString error:", e); return "{}"; } }  }
WXJsonUtils { public @NonNull static String fromObjectToJSONString(Object obj, boolean WriteNonStringKeyAsString){ try { if(WriteNonStringKeyAsString) { return JSON.toJSONString(obj, SerializerFeature.WriteNonStringKeyAsString); }else { return JSON.toJSONString(obj); } }catch(Exception e){ if(WXEnvironment.isApkDebugable()){ throw new WXRuntimeException("fromObjectToJSONString parse error!"); } WXLogUtils.e("fromObjectToJSONString error:", e); return "{}"; } }  @NonNull static List<T> getList(String json, Class<T> clazz); @NonNull static String fromObjectToJSONString(Object obj, boolean WriteNonStringKeyAsString); @NonNull static String fromObjectToJSONString(Object obj); static void putAll(Map<String, Object> container, JSONObject rawValue); }
WXJsonUtils { public @NonNull static String fromObjectToJSONString(Object obj, boolean WriteNonStringKeyAsString){ try { if(WriteNonStringKeyAsString) { return JSON.toJSONString(obj, SerializerFeature.WriteNonStringKeyAsString); }else { return JSON.toJSONString(obj); } }catch(Exception e){ if(WXEnvironment.isApkDebugable()){ throw new WXRuntimeException("fromObjectToJSONString parse error!"); } WXLogUtils.e("fromObjectToJSONString error:", e); return "{}"; } }  @NonNull static List<T> getList(String json, Class<T> clazz); @NonNull static String fromObjectToJSONString(Object obj, boolean WriteNonStringKeyAsString); @NonNull static String fromObjectToJSONString(Object obj); static void putAll(Map<String, Object> container, JSONObject rawValue);  }