0

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.

asked Feb 28, 2016 at 3:34
1
  • 1
    Can you share the json file content? Commented Feb 28, 2016 at 3:35

2 Answers 2

1

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}]}}}}
answered Feb 28, 2016 at 5:10
3
  • can u please specify the urls of the required jars Commented 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()Z Commented Feb 28, 2016 at 5:28
  • I used the URLs you provided in your comment to one of the other answers Commented Feb 28, 2016 at 5:29
1

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.

answered Feb 28, 2016 at 3:49
5
  • No,the content of json is the same what I'm trying to parse.And the java code is also same Commented Feb 28, 2016 at 3:56
  • 1
    Please provide a MCVE - stackoverflow.com/help/mcve - including either a Maven POM file or the URLs for all dependent JARs. Commented 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 project Commented Feb 28, 2016 at 4:21
  • Please provide the specific URLs that you used to download those JARs. Or the POM files. Commented Feb 28, 2016 at 4:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.