-1

I am new in json and I have to get the value from web service response. I have used org.json library for this.Below is the sample json value:

{"tms_guid": "9LaHmoHpmTd811R",
   "recharge_status": "100",
   "message": "Transaction Successful",
   "response_time": {
      "verifyClient": 0.0281,
      "verifyGuid": 0.8695,
      "verifyOperator": 0.8698,
      "verifyMsid": 0.8698,
      "tms_guid": 1.6971,
      "queryErr": 7.4243,
      "StoringRecharge": 7.4358,
      "UpdatingBalance": 7.448
   }
}

My parsing JSON input string is :

private final static String JSON_TEST_DATA
        = "{"
        + "   \"tms_guid\": \"9LaHmoHpmTd811R\", "
        + "   \"recharge_status\": \"100\", "
        + "   \"message\": \"Transaction Successful\", "
        + "   \"response_time\": { "
        + "      \"verifyClient\": 0.0281, "
        + "      \"verifyGuid\": 0.8695, "
        + "      \"verifyOperator\": 0.8698,"
        + "      \"verifyMsid\": 0.8698,"
        + "      \"tms_guid\": 1.6971,"
        + "      \"queryErr\": 7.4243,"
        + "      \"StoringRecharge\": 7.4358,"
        + "      \"UpdatingBalance\": 7.448"
        + "   }"
        + "}";

public static void main(final String[] argv) throws JSONException {

    System.out.println(JSON_TEST_DATA);
    final JSONObject testObj = new JSONObject(JSON_TEST_DATA);
 System.out.println(testObj.toString());

}

Exception is as follows:

Exception in thread "main" org.json.JSONException: Expected a ':' after a key at 5 [character 6 line 1]
    at org.json.JSONTokener.syntaxError(JSONTokener.java:432)
    at org.json.JSONObject.<init>(JSONObject.java:206)
    at org.json.JSONObject.<init>(JSONObject.java:310)
    at com.kalsym.wsp.sp.icebeep.TestIceBeep.main(TestIceBeep.java:73)

I have seen similar post. But could not figure about the solution.

Community
  • 1
  • 1
Ramgau
  • 420
  • 2
  • 6
  • 16
  • I suggest you remove portion of the input until the problem goes away. This should point to the character which is the case. It would appear it doesn't like the `_` although I thought that was a valid character. Can you try removing it? – Peter Lawrey Apr 09 '16 at 12:08
  • I am not getting about the text about "remove portion of the input until the problem". – Ramgau Apr 09 '16 at 12:10

2 Answers2

1

I am not seeing any problem. I have used same your code, but i am able to execute your program :

You can see my code, I have useed same java api too.

import org.json.JSONException;
import org.json.JSONObject;

public class TestCode {

    private final static String JSON_TEST_DATA
    = "{"
    + "   \"tms_guid\": \"9LaHmoHpmTd811R\", "
    + "   \"recharge_status\": \"100\", "
    + "   \"message\": \"Transaction Successful\", "
    + "   \"response_time\": { "
    + "      \"verifyClient\": 0.0281, "
    + "      \"verifyGuid\": 0.8695, "
    + "      \"verifyOperator\": 0.8698,"
    + "      \"verifyMsid\": 0.8698,"
    + "      \"tms_guid\": 1.6971,"
    + "      \"queryErr\": 7.4243,"
    + "      \"StoringRecharge\": 7.4358,"
    + "      \"UpdatingBalance\": 7.448"
    + "   }"
    + "}";

    public static void main (String arg[]) throws JSONException{

        //System.out.println(JSON_TEST_DATA);
        final JSONObject testObj = new JSONObject(JSON_TEST_DATA);

        System.out.println(" --"+testObj.getString("recharge_status")+"\n");
    System.out.println(testObj.toString());
    }

}

may be some char-set problem.

Gautam
  • 3,707
  • 5
  • 36
  • 57
1

I just had a similar problem, turned out that the JSON string copied from another place contained non-breaking space characters, hard to notice even under a debugger :). I was removing HTML tags from that string (received via email) with:

jstr = jstr.replaceAll("<.*?>", ""); // removes anything between < and >

and the result looked like valid JSON, but with these non-breaking spaces... This helped:

jstr = jstr.replaceAll("<.*?>|\u00a0", "");
gregko
  • 5,642
  • 9
  • 49
  • 76