Reputation: 5504
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]":""}
Upvotes: 1
Views: 1039
Reputation: 5504
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.
Upvotes: 0
Reputation: 725
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.
Upvotes: 0
Reputation: 3814
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]": ""
}
Upvotes: 1