0

I have been trying to parse a JSON file. It was somewhat out of structure, so , to get in a structure I am using a subsequence of that file (so that it gets in a structure). But when I am using subsequence or substring, then it is just omitting the starting(first offset) and not the end offset(end is as it is). Here is the code:

String url = "http://earthquake.usgs.gov/earthquakes/feed/geojsonp/2.5/week"; //json file
String response=""; 
response = getInputStreamFromUrl(url); //getting the response
response= response.subSequence(16, response.length()-2).toString(); //i want to omit the last 2 characters from the string
  txt.setText(response);   //setting the response in a text view(doing it in android)  

I have also tried counting the characters and then setting the end offset, but still it is omitting more than the specified characters.

Devyani
  • 43
  • 9
  • Is there a character such as a newline on the end of the string that is unaccounted for? – rgettman Sep 10 '14 at 20:40
  • Have you tried using `substring`? – Hot Licks Sep 10 '14 at 20:54
  • Add some code to println the `(int)response.charAt(..)` of last few characters of the string. – Hot Licks Sep 10 '14 at 20:56
  • @HotLicks When I am trying to print charAt(response.length()), it is showing java.lang.StringIndexOutOfBoundsException:length=183231;index=185423 Is the string is too long or there is another problem? And yes, I have tried substring too – Devyani Sep 11 '14 at 06:45
  • @rgettman I really don't know . The string is a JSON file and it ends with ); I have to remove these two characters from the end – Devyani Sep 11 '14 at 06:46
  • That's odd, it should only report the index being 1 larger than the length if you did that. (If length is 5, index values are 0 through 4.) – Hot Licks Sep 11 '14 at 12:00
  • (I'm guessing the above is not your real code and you have two variables, one named "response" and one named "respons", or something along that line, and you're not consistently using one or the other.) – Hot Licks Sep 11 '14 at 12:08
  • No, There is only one variable "response". I have done it by replacing the last two characters with '\0' – Devyani Sep 11 '14 at 17:00

0 Answers0