java - 如何将 jsonobject 转换为 hashmap

标签 java json

我正在使用 jackson 将 jsonobject 转换为 map。但我遇到以下错误:

org.codehaus.jackson.JsonParseException: Unexpected character ('h' (code 104)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')

如何解决这个问题?谁能帮我解决这个问题...

我的代码:

public class DataParse {


        public static void main(String a[]){

            String FILEPATH = "C:/SimpleMapping.json"; 

            Map<String,Object> resultMap = new HashMap<String,Object>();
            ObjectMapper mapperObj = new ObjectMapper();

            System.out.println("Input Json: "+FILEPATH);
            try {
                resultMap = mapperObj.readValue(new File(FILEPATH),
                                new TypeReference<HashMap<String,Object>>(){});
                System.out.println("Output Map: "+resultMap);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    }

}

我的 simple.json:

{
    "routings": {
        "routing1": {
            "targetCollection-name": "EmployeeData",
            "sourcetables-data": {
                "Employee": [{
                    "name": "employeeId",
                    "sourceDataType": "number",
                    "targetField": "employeeId",
                    "targetDataType": "double"
                }, {
                    "name": "firstName",
                    "sourceDataType": "varchar2",
                    "targetField": "firstName",
                    "targetDataType": "string"
                }, {
                    "name": "lastName",
                    " sourceDataType": "varchar2",
                    "targetField": "lastName",
                    "targetDataType": "string"
                }, {
                    "name": "contactNumber1",
                    "sourceDataType": "number",
                    "targetField": "contactNumbers",
                    "targetDataType": "array"
                }],
                "department": [{
                    "name": "departmentNumber",
                    "sourceDataType": "number",
                    "targetField": "departmentNumber",
                    "targetDataType": "double"
                }, {
                    "name": "departmentType",
                    "sourceDataType": "number",
                    "targetField": "departmentType",
                    "targetDataType": "double"
                }, {
                    "name": "startDate",
                    "sourceDataType": "timestamp",
                    "targetField": "startDate",
                    "targetDataType": "date"
                }],
                "foriegnkey": [{
                    "parentTable": "Employee",
                    "parentkey": "employeeId",
                    "childTable": "department",
                    "childKey": "empId"
                }]
            }
        }
    }

}

我有 simple.json 文件。现在尝试将此 jsonObject 转换为 map。但面临上述错误。

最佳答案

这是您的代码,但也包含必要的导入。请注意,我使用了双反斜杠(对 Windows 路径中的反斜杠进行转义),而您使用的是常规正斜杠。否则代码不变。它编译并产生预期的输出。

package json2;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

public class JSON {

    public static void main(String a[]){

        String FILEPATH = "D:\\User\\Documents\\Eclipse\\JSON2\\simplemapping.txt"; 

        Map<String,Object> resultMap = new HashMap<String,Object>();
        ObjectMapper mapperObj = new ObjectMapper();

        System.out.println("Input Json: "+FILEPATH);
        try {
            resultMap = mapperObj.readValue(new File(FILEPATH),
                            new TypeReference<HashMap<String,Object>>(){});
            System.out.println("Output Map: "+resultMap);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

-

Input Json: D:\User\Documents\Eclipse\JSON2\simplemapping.txt
Output Map: {routings={routing1={targetCollection-name=EmployeeData, sourcetables-data={Employee=[{name=employeeId, sourceDataType=number, targetField=employeeId, targetDataType=double}, {name=firstName, sourceDataType=varchar2, targetField=firstName, targetDataType=string}, {name=lastName,  sourceDataType=varchar2, targetField=lastName, targetDataType=string}, {name=contactNumber1, sourceDataType=number, targetField=contactNumbers, targetDataType=array}], department=[{name=departmentNumber, sourceDataType=number, targetField=departmentNumber, targetDataType=double}, {name=departmentType, sourceDataType=number, targetField=departmentType, targetDataType=double}, {name=startDate, sourceDataType=timestamp, targetField=startDate, targetDataType=date}], foriegnkey=[{parentTable=Employee, parentkey=employeeId, childTable=department, childKey=empId}]}}}}

关于java - 如何将 jsonobject 转换为 hashmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35678398/

相关文章:

javascript - 系统.数据.实体.动态代理

python - 如何将 CSV 文件转换为多行 JSON?

java - Java编译器错误中的动态转换

java - 无法将文档插入 mongodb 数据库会出现 NetworkOnMainthread 异常

java - 我无法让我的 2d 游戏对象移动

java - 如何重构错误移动字段类型?

php - MySql 还是 Json 用于多请求?

javascript - 通过 ng-model 将键值对传递给函数

java - 需要按 'R' 、 'B' 、 'W' 的顺序对任意长度的字符数组进行排序

jquery - 有关 JSON 实际限制的反馈?