1

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();

5 Answers 5

2

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]));
}
Sign up to request clarification or add additional context in comments.

6 Comments

but i get an error: org.json.JSONException: Value 1333333454510|-7611868|-7222457at items of type java.lang.String cannot be converted to JSONObject
@Pheonix7 As Ashwin mention in his code. use json.getString().
nice!! 1 more thing. i need to do operations with these id's, like adding and subtracting. can i just do that on the longs values?
Yes you can do addition and subtraction on long values.
as these valuse are id's and they are all subtracted by each other could u show me how to rite this is code? thanks
|
0

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("\\|");

Comments

0
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.

1 Comment

json object should be like this. {"status":0,"items":"1333333454510|-7611868|-7222457"}
0
JsonObject json= new JsonObject(response);

String item=json.getString("items");
String [] items =item.split("|");

Comments

0

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:)

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.