-1

I have Json String Object as below.

"{\"SuccessData\":\"Data fetched successfully\",\"ErrorData\":\"\",\"AppData\":\"[{\\\"uniqe_id\\\":{\\\"appId\\\":4,\\\"agentId\\\":1,\\\"isActive\\\":1\\\"},\\\"pid\\\":2223,\\\"appName\\\":ACMP\\\"},{\\\"uniqe_id\\\":{\\\"appId\\\":5,\\\"agentId\\\":1,\\\"isActive\\\":1\\\"},\\\"pid\\\":2225,\\\"appName\\\":ICMP\\\"}]\"}"

I want to convert this string to JSON object using java.

I have already tried,

JSONObject jsonObj = new JSONObject(response);

I'm getting an error saying,

   org.json.JSONException: A JSONObject text must begin with '{'
Meet Patel
  • 59
  • 2
  • 12
  • Good that you have tried something! Do remember though that we are not clairvoyant, so please tell us whether your try worked, and if not, why not (does it error, are the results in correct, does your computer explode...?) Please [edit] your post with more details, preferably also with sample output and input. – Adriaan Nov 14 '19 at 13:12
  • @Meet Patel what do you want to achieve? if you want to work with tghis structure in the future you better need to deserialize it into POJO class, for example. – Vault23 Nov 14 '19 at 13:16
  • I have just tried running this code with the string above and it seems to work fine for me? I assume you're using the org.json:json library? – Ben Green Nov 14 '19 at 13:16
  • Take a look at gson or jackson library if you are going to be working with JSON a lot. Give you the possibility to convert to and back easily. – Prashant Khanal Nov 14 '19 at 13:18
  • Make sure you are using org.json library not google-json. – Chandan Kumar Nov 14 '19 at 13:19
  • https://stackoverflow.com/a/5245881/4007703 – Somil Garg Nov 14 '19 at 13:24
  • Use JsonUtil.sanitize(JsonString) from com.google.json.JsonSanitizer first than proceed with your steps – Somil Garg Nov 14 '19 at 13:35
  • Hi @BenGreen, Yes, I'm using org.json library. And i'm getting an error as described in question. – Meet Patel Nov 15 '19 at 05:16

2 Answers2

0
"{\"SuccessData\": \"Data fetched successfully\",
  \"ErrorData\": \"\",
  \"AppData\": \"[{\\\"uniqe_id\\\":{\\\"appId\\\":4,\\\"agentId\\\":1,\\\"isActive\\\":1\\\"},\\\"pid\\\":2223,\\\"appName\\\":ACMP\\\"},{\\\"uniqe_id\\\":{\\\"appId\\\":5,\\\"agentId\\\":1,\\\"isActive\\\":1\\\"},\\\"pid\\\":2225,\\\"appName\\\":ICMP\\\"}]\"
}"

The real problem here is that this input is not valid JSON.

Let's assume that these are the exact characters that you got in your response; i.e. the first character is a double-quote. But a valid JSON object starts with a { character. Not even whitespace is allowed according to strict reading of the syntax graph at https://json.org.


But what if that is actually a Java String literal representing the JSON?

In that case, the JSON is valid1. And what is more, your code for the JSON is correct. when I compile and run this, it works ... without throwing an exception.

import org.json.JSONObject;

public class Test {
    public static void main(String[] args) {
        String response = "{\"SuccessData\":\"Data fetched successfully\",\"ErrorData\":\"\",\"AppData\":\"[{\\\"uniqe_id\\\":{\\\"appId\\\":4,\\\"agentId\\\":1,\\\"isActive\\\":1\\\"},\\\"pid\\\":2223,\\\"appName\\\":ACMP\\\"},{\\\"uniqe_id\\\":{\\\"appId\\\":5,\\\"agentId\\\":1,\\\"isActive\\\":1\\\"},\\\"pid\\\":2225,\\\"appName\\\":ICMP\\\"}]\"}";
    JSONObject jsonObj = new JSONObject(response);
    }
}

Ergo, if you are getting a JSONException then the input is not a Java String literal.


1 - I wouldn't say it was correct. The AppData attribute has a value that is a string not a JSON object. But that string is a JSON serialization. This is technically valid, but it is a poor design choice.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0
I tried with the following solution and it is working,

        import com.fasterxml.jackson.databind.ObjectMapper;

        private JSONObject deserializeResponse(String response) {
           logger.info("Parsing Serialized response object to JSON object");
           JSONObject responseJson = new JSONObject();
           ObjectMapper mapper = new ObjectMapper();
           try {
               responseJson = mapper.readValue(response.toString(), 
JSONObject.class);
           } catch (JsonGenerationException e) {
               e.printStackTrace();
           } catch (JsonMappingException e) {
               e.printStackTrace();
           } catch (IOException e) {
               e.printStackTrace();
           }
           return responseJson;
        }
Meet Patel
  • 59
  • 2
  • 12
  • This isn't the complete answer. You must have fixed something else as well. The example input you gave won't work with this code either. – Stephen C Nov 18 '19 at 11:54
  • I've added the deseralize function. Nothing else to show more. – Meet Patel Nov 19 '19 at 06:08
  • OK, so look at my Answer, copy the code to a file, compile and run it (with the json.org JAR) and please explain tell me if it works for you (like it works for me). Then explain the difference between that and what you were actually doing that didn't work. – Stephen C Nov 19 '19 at 07:28