我使用jackson将jsonobject转换为map.But我面临以下错误:
#include <stdio.h>
#include <stdlib.h> // for malloc/free
int concatenate (char *buffer, int buffsize, const char *s1, const char *s2) {
int npos = 0;
// concatenate s1 to buffer
while (*s1 != '\0') {
*buffer = *s1;
buffer++;
s1++;
++npos;
}
// concatenate s2 to buffer
while (*s2 != '\0') {
*buffer = *s2;
buffer++;
s2++;
++npos;
}
buffer = '\0';
return npos;
}
int main () {
const int buff_path = 64;
char *buffer;
int ret;
buffer = (char*)malloc (sizeof(char)*buff_path);
ret = concatenate (buffer, buff_path, "hello", " world");
printf ("This is the string: [%s]\nstring len: %i\n", buffer, ret);
free (buffer);
return 0;
}
如何解决此问题?任何人都可以帮我解决这个问题...
我的代码:
org.codehaus.jackson.JsonParseException: Unexpected character ('h' (code 104)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
我的simple.json:
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文件。现在尝试将此jsonObject转换为map.But面临上述错误。
答案 0 :(得分:1)
错误消息表示您尝试解析的内容中存在JSON语法错误。
您向我们展示的JSON不包含任何会导致该错误的内容。 (至少,不是我能看到......)
因此,我怀疑你的代码实际上正在解析不同的东西。也许你的文件名错了?也许您已经向我们展示了错误的JSON内容。也许你向我们展示了错误的代码? (或者代码中实际出现问题的代码不同。)
怀疑您向我们显示错误代码的一个原因是您向我们展示的代码没有编译。
答案 1 :(得分:1)
这是您的代码,但也有必要的导入。请注意,我使用了双反斜杠(在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}]}}}}