0

I am receiving a JSON using a servlet as below:

    try {
        JSONObject jsonObject = HTTP.toJSONObject(jb.toString());
        System.out.println("JSON STRING RECEIVED**************** " + jb.toString());//DEBUG LINE
        String TransID = jsonObject.getString("TransID");
        String TransTime = jsonObject.getString("TransTime");
        String TransAmount = jsonObject.getString("TransAmount");
        String BusinessShortCode = jsonObject.getString("BusinessShortCode");
        String BillRefNumber = jsonObject.getString("BillRefNumber");
        String MSISDN = jsonObject.getString("MSISDN");
        String FirstName = jsonObject.getString("FirstName");
        String MiddleName = jsonObject.getString("MiddleName");
        String LastName = jsonObject.getString("LastName");
        int notificationId = Notification.addRawNotitifcation(TransID, TransTime, Double.parseDouble(TransAmount), BusinessShortCode, BillRefNumber, MSISDN, FirstName, MiddleName, LastName, new Date(), "N");
        if (notificationId > 0) {
            log.debug("Message Transaction Id " + TransID + " Received SuccessFully");
        } else {
            log.debug("Error Processing Transaction Id " + TransID);
        }
        response.setContentType("application/json");

    } catch (JSONException asd) {
        // crash and burn
        log.debug(asd.getMessage());
        throw new IOException("Error parsing JSON request string");
    }
}

on the DEBUG LINE I receive the following JSON

{
   "TransactionType":"",
   "TransID":"MDE51H4CZB",
   "TransTime":"20180414104546",
   "TransAmount":"100.00",
   "BusinessShortCode":"600534",
   "BillRefNumber":"1131700102",
   "InvoiceNumber":"",
   "OrgAccountBalance":"",
   "ThirdPartyTransID":"",
   "MSISDN":"254708374149",
   "FirstName":"John",
   "MiddleName":"J.",
   "LastName":"Doe"
}

EDIT:

System.out.println(jsonObject.toString());

returns:

{  
   "Request-URI":"TransactionType",
   "Method":"{",
   "HTTP-Version":":",
   "\"\",            \"TransID\"":"\"MDE71H4CZD\",            \"TransTime\": \"20180414115011\",            \"TransAmount\": \"100.00\",            \"BusinessShortCode\": \"600534\",            \"BillRefNumber\": \"1131700102\",            \"InvoiceNumber\": \"\",            \"OrgAccountBalance\": \"\",            \"ThirdPartyTransID\": \"\",            \"MSISDN\": \"254708374149\",            \"FirstName\": \"John\",            \"MiddleName\": \"J.\",            \"LastName\": \"Doe\"        }"
}

But I am getting Error JSONObject ["TransID"] not found. What am I missing?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Stanley Mungai
  • 4,044
  • 30
  • 100
  • 168
  • Were you able to get the other values ? – v1shnu Apr 14 '18 at 08:29
  • No, execution Stops while attempting to read the First Value `TransID` – Stanley Mungai Apr 14 '18 at 08:36
  • Can you try commenting that line out and see if the other values are read ? Also try debugging your JSONObject . What you are printing is the jb. Try printing JSONObject to the console and see if you are able to see the values. – v1shnu Apr 14 '18 at 08:36
  • Post, and read, the complete stack trace of the exception. Always. It tells you what, and where the problem is. That's a much better starting point than a pure guess. – JB Nizet Apr 14 '18 at 08:53
  • And post the **actual** output you get. Your `jb.toString()` can't possibly contain new lines, so what you posted is not the actual output. – JB Nizet Apr 14 '18 at 08:56
  • @v1shnu I have edited the question with the output of the jsonObject. When I comment on that line the error goes to the next line – Stanley Mungai Apr 14 '18 at 08:58
  • @JBNizet That Is the actual Output I have just formatted it for readability. – Stanley Mungai Apr 14 '18 at 08:58
  • Looks like your JSONObject is not like the way you want it. Can you post the full `jsonObject.toString()` output. But the look of this, it looks like your actual JSON data is encoded as a string. Try to get that String first and then parse it into a JSON Object. – v1shnu Apr 14 '18 at 09:06
  • 1
    Instead of doing all this, you can use something like `Google Gson` or `ObjectMapper` to easily parse your data. – v1shnu Apr 14 '18 at 09:07
  • Check out this question , you will be able to figure out what exactly that you want to do - https://stackoverflow.com/questions/1548782/retrieving-json-object-literal-from-httpservletrequest – v1shnu Apr 14 '18 at 09:08
  • You expect `HTTP.toJSONObject(jb.toString());` to parse the given String. But it clearly doesn't do that. So you need to fix that method, or to call another one, that indeed parses the JSON. – JB Nizet Apr 14 '18 at 09:32
  • @v1shnu Thank you. Google GSON `JsonElement` and `JsonObject` worked like charm. – Stanley Mungai Apr 14 '18 at 10:38
  • @ErrorNotFoundException glad it helped :) – v1shnu Apr 14 '18 at 10:40

0 Answers0