Reputation: 2191
This is the JsonObject i get form parsing my url: {"status":0,"items":"1333333454510|-7611868|-7222457"
now i need to get the seperate numbers seperetly as long
.
i also get an error if i try to convert it to string: org.json.JSONException: Value 1333333454510|-7611868|-7222457 at items of type java.lang.String cannot be converted to JSONObject
How? Thanks
here is some code
JSONObject obj = JSONParser.getJSONFromUrl(URL + "search", parameters);
String firstItem = obj.getJSONObject("items").toString();
Upvotes: 1
Views: 110
Reputation: 993
your problem is here
JSONObject obj = JSONParser.getJSONFromUrl(URL + "search", parameters);
String firstItem = obj.getJSONObject("items").toString();
so change it to
String firstItem = obj.getString("items").toString();
and then do split operations as suggested by others.
SO force always with you:)
Upvotes: 0
Reputation: 9507
Split your items
using \\|
.
Example :
String items= "1333333454510|-7611868|-7222457" ; //here you got items
String[] _items=items.split("\\|");
Now iterate loop and convert it into long using Long.ParseLong().
EDIT :
As Ashwin said you need to use json.getString("items");
to get items.
You can convert it into long using following code.
ArrayList<Long> longs=new ArrayList<Long>();
for(int i=0;i<_items.length;i++){
longs.add(Long.parseLong(_items[i]));
}
Upvotes: 2
Reputation: 1486
JsonObject json= new JsonObject(response);
String item=json.getString("items");
String [] items =item.split("|");
Upvotes: 0
Reputation: 3663
String response= "{"status":0,"items":"1333333454510|-7611868|-7222457"}" ; // ie, response contains the string.
JsonObject json= new JsonObject(response);
String item=json.getString("items");
String [] items =item.split("|");
Will get 3 numbers in the items array.
Upvotes: 0
Reputation: 1996
Use Split function to separate.
parse json to get items in items String
use jsonObject.getString instead of jsonObject.getJSONObject to avoid error.
String items= "1333333454510|-7611868|-7222457" ;
String[] itemsArray =items.split("\\|");
Upvotes: 0