1

I am getting an error while reading the json data from URL. Below is the code what i am trying to.Please correct me where i am going wrong.

public class ReadingJsonData {
 public static void main(String[] args) throws JSONException {
 JSONObject json = readJsonFromUrl("http://requestb.in/pp1mzapp");
  }

public static JSONObject readJsonFromUrl(String url) throws IOException,  JSONException {
     InputStream is = new URL(url).openStream();
     try {
       BufferedReader rd = new BufferedReader(new InputStreamReader(is,  Charset.forName("UTF-8")));
       String jsonText = readAll(rd);
       JSONObject json = new JSONObject(jsonText.trim());
       return json;
     } finally {
       is.close();
     }
    }

     private static String readAll(Reader rd) throws IOException {
       StringBuilder sb = new StringBuilder();
       int cp;
       while ((cp = rd.read()) != -1) {
        sb.append((char) cp);
       }
       return sb.toString();
    }
     }

And my Json object from URL looks like

{"FormID":"2095180","UniqueID":"213482652","Name":{"first":"Something","last":"New"},"Date of Birth":"Feb 03, 1926","Last 4 Digits of SSN":"1234","Week Beginning Date":"Jan 01, 2012","Week Ending Date":"Feb 03, 2014","Email":"[email protected]":""} 
0

3 Answers 3

1

Your Json is not correct, see the last line

{
"FormID": "2095180",
"UniqueID": "213482652",
"Name": {
    "first": "Something",
    "last": "New"
},
"Date of Birth": "Feb 03, 1926",
"Last 4 Digits of SSN": "1234",
"Week Beginning Date": "Jan 01, 2012",
"Week Ending Date": "Feb 03, 2014",
"Email": "[email protected]": ""
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thats a TYPO, I fixed it
0

Actually what I get when opening this URL is "ok". That's the reason why it complains that your Json string doesn't start with a curly bracket.

2 Comments

Yes when i tried in difffrent way its just display's OK. But when you try open a page with "requestb.in/pp1mzapp" it shows the json objcet
You should print the contents of jsonText to the console to make sure that you're actually getting the Json string you expect.
0

I see the issue, the URL what i am passing is getting the HTML page, entire page instead of the response body that is the issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.