I'm using jackson to convert jsonobject to map.But I'm facing the following error:
org.codehaus.jackson.JsonParseException: Unexpected character ('h' (code 104)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
How can I resolve this issue? Can anyone please help me out regarding this issue ...
My code :
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();
}
}
}
My 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"
}]
}
}
}
}
I have simple.json file.Now trying to convert this jsonObject to map.But facing the above error.
-
1Can you share the json file content?Darshan Mehta– Darshan Mehta02/28/2016 03:35:37Commented Feb 28, 2016 at 3:35
2 Answers 2
This is your code, but with the necessary imports as well. Note that I've used double backslash (escaping of the backslashes in the Windows path), while you used a regular forward slash. Otherwise the code is unchanged. It compiles and produces the expected output.
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}]}}}}
-
can u please specify the urls of the required jarsdev777– dev77702/28/2016 05:27:39Commented Feb 28, 2016 at 5:27
-
I have used the above specified urls.But I'm getting the following error: Exception in thread "main" java.lang.NoSuchMethodError: org.codehaus.jackson.type.JavaType.isMapLikeType()Zdev777– dev77702/28/2016 05:28:46Commented Feb 28, 2016 at 5:28
-
I used the URLs you provided in your comment to one of the other answersMads Nielsen– Mads Nielsen02/28/2016 05:29:51Commented Feb 28, 2016 at 5:29
The error message implies that there is a JSON syntax error in the stuff your are trying to parse.
The JSON you showed us does not contain anything that would cause that error. (At least, not that I can see ...)
Therefore, I suspect that your code is actually parsing something different. Maybe you got the filename wrong? Maybe you have shown us the wrong JSON content. Maybe you have shown us the wrong code? (Or different code to the code where the problem actually occurs.)
One reason to suspect that you have show us the wrong code is that the code that you have shown us DOES NOT COMPILE.
-
No,the content of json is the same what I'm trying to parse.And the java code is also samedev777– dev77702/28/2016 03:56:02Commented Feb 28, 2016 at 3:56
-
1Please provide a MCVE - stackoverflow.com/help/mcve - including either a Maven POM file or the URLs for all dependent JARs.Stephen C– Stephen C02/28/2016 04:07:29Commented Feb 28, 2016 at 4:07
-
I have used json-simple-1.1.jar and jackson-all-1.7.3.jar as dependencies to my java projectdev777– dev77702/28/2016 04:21:37Commented Feb 28, 2016 at 4:21
-
Please provide the specific URLs that you used to download those JARs. Or the POM files.Stephen C– Stephen C02/28/2016 04:48:30Commented Feb 28, 2016 at 4:48
-
java2s.com/Code/Jar/j/Downloadjsonsimple11jar.htm, java2s.com/Code/Jar/j/Downloadjacksonall173jar.htmdev777– dev77702/28/2016 04:55:52Commented Feb 28, 2016 at 4:55