1

I am trying to store JSON data into a POJO class and then store the class into Firebase. If I use str1 as input to fromJson the pojo class is populated properly, but if str2 is used fromJson returns null. Question is which of the two strings(str1 or str2) is correct ? My original data is in CSV format and I used one of the online converters to convert the CSV file to JSON format. The data is identical to the format shown in str2.

String str1 =  "{\"code\":602,\"desc\":\"somedescription\"}"; 
String str2 =  "{"code":602,"desc":"somedesc"}";
myGson = new Gson();
codeDescMast = myGson.fromJson(str1,CodeDescMast.class);

This is my POJO class.

public class CodeDescMast {
   int code;
   String desc;

public CodeDescMast() { }
public CodeDescMast(int code, String desc) {
    this.code = code;
    this.desc = desc;
}

public int getCode() {
    return code;
}

public void setCode(int code) {
    this.code = code;
}

public String getDesc() {
    return desc;
}

public void setDesc(String desc) {
    this.desc = desc;
}

}

regards jana

megjosh
  • 101
  • 1
  • 9
  • ```\"``` is a escape character for a string literal quote inside the text, the second option is not compilable by code, when loading from a txt file the string will have it as a escape anyway, you need to show the content of the file and how you load it into memory – Marcos Vasconcelos Apr 16 '18 at 18:40
  • str1 is correct, you should escape double quotes in strings in Java. Did your code really compile when you have this `String str2 = "{"code":602,"desc":"somedesc"}";` in your code? – Sony Apr 16 '18 at 18:41
  • @Marcos I have a dataset of CSV values. Using an online service I converted the CSV to JSON . The output format of this service was a string like str2. The following is the way I read JSON file InputStream is = context.getAssets().open("samp.json") ; int size = is.available() ; byte[] buffer = new byte[size] ; is.read(buffer); is.close(); codeDescJSON = new String(buffer,"UTF-8") ; The variable codeDescJSON is str2 that I am passing to fromJson() method . The question is "Should double quotes in JSON be escaped or is it optional ?" – megjosh Apr 17 '18 at 10:45
  • @Sony There are no crashes but the code and desc contain null values – megjosh Apr 17 '18 at 10:48
  • No it should not be escaped when reading from a text stream, you need to debug to see if you are loading it correctly – Marcos Vasconcelos Apr 17 '18 at 13:51
  • @Marcos the following is my git-hub repository of my code https://github.com/ariyurjana/csvtojsontoFbase/tree/master. May be you can have a look to check if I am loading it correctly . I extracted only one item of the json and tested this code when the whole file did not work properly – megjosh Apr 17 '18 at 17:53
  • You need to learn how to de bug and see the runtime values of your variable – Marcos Vasconcelos Apr 17 '18 at 18:00
  • @Marcos Yeah I know how to do that . The darkhose is the online service I am using to get the JSON from csv. I will write that part of the code and include the backslash along with double quote which will solve my issue. – megjosh Apr 17 '18 at 18:18
  • You can debug the result returned by the online service and check if everything from there is working – Marcos Vasconcelos Apr 17 '18 at 18:32

1 Answers1

0

Gson is correctly processing str1 because its is syntactically correct in java.

however str2 is incorrect, infact it shouldn't compile too.

String str1 =  "{\"code\":602,\"desc\":\"somedescription\"}"; // Correct Way
String str2 =  "{"code":602,"desc":"somedesc"}"; // Incorrect Way

We can use the \ character to indicate that we want to include a special character, and that the next character should be treated differently. \" indicates a quote character, not the termination of a string.

See this thread on such implementation.

sam
  • 1,800
  • 1
  • 25
  • 47
  • In Java the double quotes needs to be escaped and in others they need not be escaped. Is my understanding correct ? – megjosh Apr 17 '18 at 10:51
  • if you found my answer useful then you can mark it as a solution and upvote it, so that it may help others too. – sam Apr 17 '18 at 13:39