解决对象与JSON解析时属性不对应+空字符串+枚举的问题

当JSON字符串解析成对象时,遇到属性不匹配或枚举值为空字符串会报错。本文提供了解决此类问题的代码配置方案。
摘要由CSDN通过智能技术生成

在将JSON串解析为对象时,如果属性不对应或者枚举为空字符串时,均会导致解析错误,此时可通过几个配置解决,直接上代码

package com.dms.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtils {

	public static String encode(Object obj) {
		ObjectMapper om = new ObjectMapper();
		ByteArrayOutputStream baos = null;
		try {
			baos = new ByteArrayOutputStream();
			JsonGenerator generator = om.getFactory().createGenerator(baos,
					JsonEncoding.UTF8);
			generator.writeObject(obj);
			String result = new String(baos.toByteArray(), "utf-8");
			return result;
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			try {
				if (baos != null) {
					baos.close();
				}
		
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个使用C语言编写的简单的JSON解析器,可以解析符合JSON语法规则的字符串,并将其解析对应的数据结构。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // JSON数据类型枚举 enum json_type { JSON_OBJECT, JSON_ARRAY, JSON_STRING, JSON_NUMBER, JSON_BOOLEAN, JSON_NULL }; // JSON节点结构体 struct json_node { enum json_type type; // 节点类型 union { char *string; // 字符串类型 double number; // 数字类型 int boolean; // 布尔类型 struct json_node *elements; // 数组类型 struct { char *key; struct json_node *value; } *members; // 对象类型 } data; int size; // 数组或对象的大小 }; // 创建一个字符串节点 struct json_node *create_string_node(char *value) { struct json_node *node = (struct json_node *)malloc(sizeof(struct json_node)); node->type = JSON_STRING; node->data.string = (char *)malloc(strlen(value) + 1); strcpy(node->data.string, value); return node; } // 创建一个数字节点 struct json_node *create_number_node(double value) { struct json_node *node = (struct json_node *)malloc(sizeof(struct json_node)); node->type = JSON_NUMBER; node->data.number = value; return node; } // 创建一个布尔节点 struct json_node *create_boolean_node(int value) { struct json_node *node = (struct json_node *)malloc(sizeof(struct json_node)); node->type = JSON_BOOLEAN; node->data.boolean = value; return node; } // 创建一个节点 struct json_node *create_null_node() { struct json_node *node = (struct json_node *)malloc(sizeof(struct json_node)); node->type = JSON_NULL; return node; } // 解析一个字符串 char *parse_string(char *json_string, int *pos) { char *value = NULL; int start = ++(*pos); while (json_string[*pos] != '"') { (*pos)++; } int end = *pos; int len = end - start; if (len > 0) { value = (char *)malloc(len + 1); strncpy(value, json_string + start, len); value[len] = '\0'; } (*pos)++; return value; } // 解析一个数字 double parse_number(char *json_string, int *pos) { double value = 0.0; int start = *pos; while (json_string[*pos] == '-' || (json_string[*pos] >= '0' && json_string[*pos] <= '9') || json_string[*pos] == '.' || json_string[*pos] == 'e' || json_string[*pos] == 'E') { (*pos)++; } int end = *pos; int len = end - start; if (len > 0) { char *number_str = (char *)malloc(len + 1); strncpy(number_str, json_string + start, len); number_str[len] = '\0'; value = atof(number_str); free(number_str); } return value; } // 解析一个数组 struct json_node *parse_array(char *json_string, int *pos) { struct json_node *node = (struct json_node *)malloc(sizeof(struct json_node)); node->type = JSON_ARRAY; node->size = 0; node->data.elements = NULL; (*pos)++; while (json_string[*pos] != ']') { if (node->size > 0) { (*pos)++; } struct json_node *element = parse_value(json_string, pos); node->size++; node->data.elements = (struct json_node *)realloc(node->data.elements, node->size * sizeof(struct json_node)); node->data.elements[node->size - 1] = *element; free(element); } (*pos)++; return node; } // 解析一个对象 struct json_node *parse_object(char *json_string, int *pos) { struct json_node *node = (struct json_node *)malloc(sizeof(struct json_node)); node->type = JSON_OBJECT; node->size = 0; node->data.members = NULL; (*pos)++; while (json_string[*pos] != '}') { if (node->size > 0) { (*pos)++; } char *key = parse_string(json_string, pos); (*pos)++; struct json_node *value = parse_value(json_string, pos); node->size++; node->data.members = (struct json_node *)realloc(node->data.members, node->size * sizeof(struct json_node)); node->data.members[node->size - 1].key = key; node->data.members[node->size - 1].value = value; } (*pos)++; return node; } // 解析一个 struct json_node *parse_value(char *json_string, int *pos) { while (json_string[*pos] == ' ' || json_string[*pos] == '\n' || json_string[*pos] == '\r' || json_string[*pos] == '\t') { (*pos)++; } if (json_string[*pos] == '"') { return create_string_node(parse_string(json_string, pos)); } else if (json_string[*pos] == '-' || (json_string[*pos] >= '0' && json_string[*pos] <= '9')) { return create_number_node(parse_number(json_string, pos)); } else if (json_string[*pos] == 't' || json_string[*pos] == 'f') { return create_boolean_node(json_string[*pos] == 't' ? 1 : 0); } else if (json_string[*pos] == 'n') { (*pos) += 4; return create_null_node(); } else if (json_string[*pos] == '[') { return parse_array(json_string, pos); } else if (json_string[*pos] == '{') { return parse_object(json_string, pos); } return NULL; } // 解析一个JSON字符串 struct json_node *parse_json_string(char *json_string) { int pos = 0; return parse_value(json_string, &pos); } // 打印一个JSON节点 void print_json_node(struct json_node *node) { switch (node->type) { case JSON_OBJECT: printf("{\n"); for (int i = 0; i < node->size; i++) { printf(" \"%s\": ", node->data.members[i].key); print_json_node(node->data.members[i].value); if (i < node->size - 1) { printf(",\n"); } } printf("\n}"); break; case JSON_ARRAY: printf("[\n"); for (int i = 0; i < node->size; i++) { printf(" "); print_json_node(&node->data.elements[i]); if (i < node->size - 1) { printf(",\n"); } } printf("\n]"); break; case JSON_STRING: printf("\"%s\"", node->data.string); break; case JSON_NUMBER: printf("%f", node->data.number); break; case JSON_BOOLEAN: printf("%s", node->data.boolean ? "true" : "false"); break; case JSON_NULL: printf("null"); break; } } // 释放一个JSON节点 void free_json_node(struct json_node *node) { if (node == NULL) { return; } switch (node->type) { case JSON_OBJECT: for (int i = 0; i < node->size; i++) { free(node->data.members[i].key); free_json_node(node->data.members[i].value); } free(node->data.members); break; case JSON_ARRAY: for (int i = 0; i < node->size; i++) { free_json_node(&node->data.elements[i]); } free(node->data.elements); break; case JSON_STRING: free(node->data.string); break; } free(node); } int main() { char *json_string = "{\"name\": \"Alice\", \"age\": 30, \"is_student\": true, \"grades\": [90, 85, 95]}"; struct json_node *root = parse_json_string(json_string); print_json_node(root); printf("\n"); free_json_node(root); return 0; } ``` 这个JSON解析器使用递归下降解析的方法,根据JSON的语法规则逐个解析JSON字符串中的字符,生成对应的节点,并将它们组合成一个完整的JSON对象。 希望这个代码可以帮助您开始编写自己的JSON解析器。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值