不同Json工具对空串和NULL的序列号处理:net.sf.json 和 fastjson

net.sf.json 和 fastjson 对于空串和NULL的处理:

1、测试代码

package com.TestMain;
 
import com.alibaba.fastjson.JSON;
 
import java.util.HashMap;
import java.util.Map;
 
public class TestTest {
 
    public static void main(String args[]) throws Exception {
 
        String sDesc = "";
        Map<String, String> map = new HashMap<String, String>();
        map.put("test", "test");
        map.put("str", null);
        System.out.println("测试:null");
        System.out.println("ALIBAB:"+JSON.toJSONString(map));
        System.out.println("net:"+net.sf.json.JSONObject.fromObject(map).toString());
        System.out.println("=====");
 
        Map<String, String> map2 = new HashMap<String, String>();
        map2.put("test", "test");
        map2.put("str", "null");
        System.out.println("测试:null字符串");
        System.out.println("ALIBAB:"+JSON.toJSONString(map2));
        System.out.println("net:"+net.sf.json.JSONObject.fromObject(map2).toString());
        System.out.println("=====");
 
        Map<String, String> map3 = new HashMap<String, String>();
        map3.put("test", "test");
        map3.put("str", "");
        System.out.println("测试:空白字符串");
        System.out.println("ALIBAB:"+JSON.toJSONString(map3));
        System.out.println("net:"+net.sf.json.JSONObject.fromObject(map3).toString());
        System.out.println("=====");
 
    }
 
}

2、测试结果:

测试:null
ALIBAB:{"test":"test"}
net:{"str":null,"test":"test"}
=====
测试:null字符串
ALIBAB:{"str":"null","test":"test"}
net:{"str":null,"test":"test"}
=====
测试:空白字符串
ALIBAB:{"str":"","test":"test"}
net:{"str":"","test":"test"}
=====

3、总结:

  • fastJson 根据传入的对象进行序列化,是字符串就是字符串,是NULL就是NULL。序列化结果不包含NULL。
  • net.sf.json 会将NULL和NULL字符串都作为NULL处理。序列化结果包含NULL,但是其值也是NULL(不是NULL字符串)。

4、注:Maven中引入net.sf.json的方式

  1. maven 无法引入 net.sf.json 的解决方法 - vili_sky 的博客 - 优快云 博客
  2. json-lib 的 maven dependency - 海山 - 博客园

转载于:https://www.cnblogs.com/buwuliao/p/11498123.html

### 解决 Python 中 `json.decoder.JSONDecodeError` 错误 当遇到 `json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)` 这样的错误时,意味着 JSON 解析器在尝试读取数据的第一行第一列(即起始位置)未能找到任何有效值[^3]。 #### 可能的原因 - 输入字符串为空或仅包含空白字符。 - 数据源中的内容并非有效的 JSON 格式。这可能是因为文件损坏、传输过程中丢失部分数据等原因造成[^5]。 #### 验证输入的有效性 为了确认问题所在,可以先打印出即将被解析的数据: ```python import json data_to_parse = "此处替换为实际要解析的内容" print(f"准备解析的数据:\n{data_to_parse}") try: parsed_data = json.loads(data_to_parse) except json.JSONDecodeError as e: print(e) ``` 如果输出显示 `data_to_parse` 是空串或者是不完整的 JSON 文本,则找到了引发异常的根本原因[^4]。 #### 处理潜在的无效输入 对于来自外部资源(如网络请求返回的结果或是 Excel 单元格内的值),应当考虑加入额外逻辑来验证其有效性并处理特殊情况: ```python def safe_json_loads(value): try: return json.loads(value.strip()) if isinstance(value, str) and value.strip() != '' else None except ValueError: return None # 假设 run_model 的值是从某个单元格获取而来 run_model_raw = opera_excel.get_cell_value(row, col) run_model = safe_json_loads(run_model_raw) if not run_model: # 当无法成功加载 JSON 或者得到的是 null/None 类型的对象时执行此分支 pass # 插入适当的操作,比如记录日志、提示用户重新上传正确格式的数据等 else: # 继续正常流程... pass ``` 通过上述方法可以在很大程度上减少因意外情况而导致程序崩溃的风险,同时也提高了代码健壮性用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值