2

I have a JSON object as below. When I try to fetch the value of name using :

String name = jsonObject.getJSONObject("result").getString("name");

In java

{ 
    "result":
    {
        "name":   "ABC",
        "dob": "12-11-1958",
        "issue_date": "01-11-2011",
        "blood_group": "",
        "father/husband": "BCD",
        "address": "53/9 ASHOK NAGAR,Delhi 110018",
        "cov_details": {
            "LMV": "01-11-2011  DY.DIR.ZONAL OFFICE,NORTH WEST DISTRICT-II,ROHINI",
            "MCWG": "01-11-2011  DY.DIR.ZONAL OFFICE,NORTH WEST DISTRICT-II,ROHINI"
        },
        "validity": {
            "non-transport": "01-11-1958 to 31-10-1978",
            "transport": ""
        } 
    } 
}

It gives me:

org.json.JSONException: A JSONObject text must begin with '{' at character 6 exception.

Paul
  • 4,160
  • 3
  • 30
  • 56
Mavericks
  • 283
  • 1
  • 7
  • 20
  • You need to debug this. First check what is in "jsonObject", if it is correct then check for jsonObject.getJSONObject("result"), if this is also as expected then check jsonObject.getJSONObject("result").getString("name"). I hope this will help. – Charu Dec 28 '16 at 10:29
  • @charu Actually You are right, the problem is while debugging i get to know that it's sending me the Internal Server error, which actually doesn't happen to be case with the same request in REST POSTMAN client. – Mavericks Dec 28 '16 at 11:12
  • Moreover when i'm making this request okhttp3.RequestBody body = okhttp3.RequestBody.create(mediaType, "{\"a_no\":\"123445566\",\"dob\":\"30-11-1992\",\"key\":\"abcd123\"}"); it's working fine, now what i'm trying to do is to pass the reference variable instead of static string, how can i do that. – Mavericks Dec 28 '16 at 11:19
  • Tidied up the code and some grammaticals. – Paul Dec 30 '16 at 15:54

2 Answers2

1

I believe you somehow did not load the right content. Here's a small program that consumes the input that you posted:

package org.json;

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder();
    for (Scanner sc = new Scanner(Main.class.getResourceAsStream("in.json")); sc.hasNext(); ) {
      sb.append(sc.nextLine()).append("\n");
    }

    JSONObject jsonObject = new JSONObject(sb.toString());
    String name = jsonObject.getJSONObject("result").getString("name");
    System.out.println("name=" + name); 
    System.out.println("jsonObject=" + jsonObject); 
  }
}

The output is, as expected:

name=ABC
jsonObject={"result":{"cov_details":{"MCWG":"01-11-2011  DY.DIR.ZONAL OFFICE,NORTH WEST DISTRICT-II,ROHINI","LMV":"01-11-2011  DY.DIR.ZONAL OFFICE,NORTH WEST DISTRICT-II,ROHINI"},"address":"53/9 ASHOK NAGAR,Delhi 110018","issue_date":"01-11-2011","dob":"12-11-1958","name":"ABC","blood_group":"","validity":{"transport":"","non-transport":"01-11-1958 to 31-10-1978"},"father/husband":"BCD"}}

Therefore, I think you have problems in loading/initializing the object pointed by the jsonObject variable.

FTR, I am using the JSON library from https://github.com/stleary/JSON-java.

Itay Maman
  • 30,277
  • 10
  • 88
  • 118
  • okhttp3.OkHttpClient client = new okhttp3.OkHttpClient(); public static void main(String[] args) {okhttp3.OkHttpClient client = new okhttp3.OkHttpClient(); MediaType mediaType = MediaType.parse("application/json"); okhttp3.RequestBody body = okhttp3.RequestBody.create(mediaType, "{\"a_no\":\"10830\",\"dob\":\"30-11-1992\",\"key\":\"abc\"}"); okhttp3.Request request = new okhttp3.Request.Builder().url("http://api.abc.com:8087/test") .post(body).build(); okhttp3.Response response = client.newCall(request).execute(); JSONObject jsonObject = new JSONObject(response.body().string()); – Mavericks Dec 28 '16 at 12:02
  • This is the program which i'm using which is providing me the correct value, Whereas while building the value with reference variable it's throwing error. – Mavericks Dec 28 '16 at 12:03
  • @Mavericks can you print the value of `jsonObject`, that is: `System.out.println(jsonObject)` ? This will make it easy to understand your problem – Itay Maman Dec 28 '16 at 12:34
  • @Mavericks also, can you print what is the value of `response.body` ? In other words, you need to run your program but change the last lines to: `okhttp3.Response response = client.newCall(request).execute(); System.out.println(response.body().string());` – Itay Maman Dec 28 '16 at 12:41
  • Thank you so much it worked after initializing the JSONObject and passing the JSONObject obj = new JSONObject(response.body().string()); – Mavericks Dec 28 '16 at 15:14
0

@Mavericks are you posting using retorfit? if yes then use compile 'com.squareup.retrofit2:converter-scalars:2.1.0' for posting plain text.

Piyush Patel
  • 371
  • 1
  • 5
  • 13
  • Not precisely i'm using com.squareup.okhttp3 version 3.4 – Mavericks Dec 28 '16 at 11:34
  • I think you have problem with slashes while making request please refer this link http://stackoverflow.com/questions/21568681/removing-in-json-object-string – Piyush Patel Dec 28 '16 at 11:45
  • thanks for your reply but this is not the case their is no any slash within my JSON as mentioned above in the sample JSON data. – Mavericks Dec 28 '16 at 11:51
  • print your reference variable it will be there its editor problem that's added extra slashes while you are creating JSON – Piyush Patel Dec 28 '16 at 11:55