I want to fetch a text (some urls) from Json and add them straightly to a String[] in android. The text is like
"http://www.androiddrawer.com/wp-content/uploads/2013/05/Subway-Surfers-screenshot-4-1.png","http://www.androiddrawer.com/wp-content/uploads/2013/05/Subway-Surfers-screenshot-4-2.png","http://www.androiddrawer.com/wp-content/uploads/2013/05/Subway-Surfers-screenshot-4-3.png","http://www.androiddrawer.com/wp-content/uploads/2013/05/Subway-Surfers-screenshot-4-4.png"
I want to put this text into a String[]. I know I can use this method:
products = json.getJSONArray(TAG_PRODUCTS);
JSONObject c = products.getJSONObject(0);
String name = c.getString(TAG_NAME);
But it gives me String not String[].
By looking your String
"http://www.androiddrawer.com/wp-content/uploads/2013/05/Subway-Surfers-screenshot-4-1.png","http://www.androiddrawer.com/wp-content/uploads/2013/05/Subway-Surfers-screenshot-4-2.png","http://www.androiddrawer.com/wp-content/uploads/2013/05/Subway-Surfers-screenshot-4-3.png","http://www.androiddrawer.com/wp-content/uploads/2013/05/Subway-Surfers-screenshot-4-4.png"
It seems that this can be converted into String[].
use like
String[] result = above_string_variable.split(",");
In your case
use
String[] result = c.getString(TAG_NAME).split(",");
instead of
String name = c.getString(TAG_NAME);
products = json.getJSONArray(TAG_PRODUCTS);
String[] result = new String[products.length()];
for(int i=0;i<products.length();i++) {
JSONObject c = products.getJSONObject(i);
result[i] = c.getString(TAG_NAME);
}
Related
I am making a request from my app to a server which is returning an array. I want to put that array into a listView but the the whole array is treated as a single listItem.
As the response is recieved in a string variable i want to convert it to a string array?
any help is appretiated.. Thanks
Just replace arr with your String Array variable name:
StringBuilder builder = new StringBuilder();
for(String s : arr) {
builder.append(s);
}
String str = builder.toString();
Firstly string response data split by space[Available special character] or assign string array.
String yourdata = "A B C D E F G H";
String[] yourtDataArr = yourdata.split(" ");
ArrayList<String> list = new ArrayList<String>();
for(i =0;i<yourtDataArr.length;i++){
list.add(yourtDataArr[i]);
}
Then use your list on ListView. You can see following example.
http://www.vogella.com/tutorials/AndroidListView/article.html
https://www.tutorialspoint.com/android/android_list_view.htm
http://windrealm.org/tutorials/android/android-listview.php
Creating an app in which i want to get string from json but i have one key and multiple value so i don't know how to handle this.
"colours": "#fff600,#000000,#ffffff,#00000,#ff9900,#333333"
And want to use this color in different class:
final ValueAnimator colorAnimation = ValueAnimator.ofObject(new android.animation.ArgbEvaluator(), Color.RED, Color.BLUE,Color.WHITE,Color.YELLOW,Color.CYAN,Color.MAGENTA,Color.GREEN,Color.GRAY);
colorAnimation.setDuration(1400);
Put the value of colours in string variable and then split the string in following way and add it to an arraylist :
String[] arr = str.split(",");
ArrayList<String> arr1 = new ArrayList<String>();
for (int i=0; i<arr.length; i++){
arr1.add(arr[i]);
}
Get value of color and use string tokenizer with ',' delima like this:
StringTokenizer stringTokenizer = new StringTokenizer(colorValueString, ",");
Also it has stringTokenizer.nextToken to get the next color in string
You can get the value of colours as a string and then split the string into parts like this:
String colours = json.getString("colours");
Log.d(TAG, colours);
String items[] = colours.split(",");
for (String item : items) {
Log.d(TAG, item);
}
If you own the json
You should use JSONArray:
"colours": ["#fff600","#000000","#ffffff","#00000","#ff9900","#333333"]
And read it like
JSONObject json = ...;
JSONArray colorsArray = json.getJSONArray("colours");
for(int i = 0; i < colorsArray.length(); i++) {
String colorString = colorsArray.getString(i);
int color = Color.parseColor(colorString);
// you should probably also catch IllegalArgumentException for wrong input
}
If you don't own the json
You can read it as string and split around commas:
JSONObject json = ...;
String colorsString = json.getString("colours");
String[] colorStrings = colorsString.split(",");
for(String string : colorStrings) {
int color = Color.parseColor(string);
// you should probably also catch IllegalArgumentException for wrong input
}
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();
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]));
}
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("\\|");
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.
JsonObject json= new JsonObject(response);
String item=json.getString("items");
String [] items =item.split("|");
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:)
I have alot of link to get data from web hence I wanted to use loop to retrieve each URL's data but I had trouble in making JSObject as array.
JSONObject[] jsObjectallnewstype;
JSONArray[] jsonArrayallnewstype = null;
for(int i = 0; i < categories.length(); i++)
{
JSONObject c = categories.getJSONObject(i);
// Storing each json item in variable
String title = c.getString(TAG_TITLE);
String url = c.getString(TAG_URL);
jsObjectallnewstype[i] = JSONFunction.getnewstype(title, url); //java.lang.NullPointerException
jsonArrayallnewstype[i] = jsobjectallnewstype[i].getJSONArray(TAG_NEWLIST);
}
This line jsObjectallnewstype[i] get null error although the log shows JSONFunction.getnewstype successfully retrieve the data.
And i am also worry tat second line jsonArrayallnewstype[i] could cause same error as well.
So JSObject cant be put as array? If so what are the alternative??
To fix your current code, you need to initialize your array. This is why you are getting a NPE:
JSONObject[] jsObjectallnewstype = new JSONObject[categories.length()];
JSONArray[] jsonArrayallnewstype = new JSONArray[categories.length()];
for(int i = 0; i < categories.length(); i++)
{
JSONObject c = categories.getJSONObject(i);
// Storing each json item in variable
String title = c.getString(TAG_TITLE);
String url = c.getString(TAG_URL);
jsObjectallnewstype[i] = JSONFunction.getnewstype(title, url); //java.lang.NullPointerException
jsonArrayallnewstype[i] = jsobjectallnewstype[i].getJSONArray(TAG_NEWLIST);
}
As a result of the two for-loops below I should get the word an Album name, but I get this name in reverse order. For example if I expect "LINK", I get "KNIL"... Where am I going wrong?
Here is the code:
jsonobj = new JSONObject(param);
JSONObject datajson = jsonobj.getJSONObject("data");
JSONArray news = datajson.getJSONArray(TAG_NEWS);
JSONArray actual = datajson.getJSONArray("actual");
for(int i = 0; i < news.length(); i++){
JSONObject c = news.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String title = c.getString(TAG_TITLE);
String album = c.getString(TAG_ALBUM);
TextView tv = (TextView) findViewById(R.id.imgtest);
//tv.setText(album);
if(!(album == "null")){
String var[] = album.split("|");
for(int a=0;a<var.length;a++){
String t = var[a].intern();
String al = ((TextView) findViewById(R.id.imgtest)).getText().toString();
String b = t+al;
tv.setText(b);
}
}else{
break;
}
}
This line is adding the newest letter t to the beginning of your existing String al:
String b = t+al;
However rather than simply switching this line around (b = al + t), I recommend a faster method:
String album = c.getString(TAG_ALBUM).replaceAll("|", "");
TextView tv = (TextView) findViewById(R.id.imgtest);
tv.setText(album);
I looks like you only wanted to remove the "|" character from your album String, so just use String.replaceAll().
If you need album split into an array, then when you rebuild the String don't waste your time calling findViewById() on a view that you already have. In fact you already have the String, so there is no need to use getText() either:
String var[] = album.split("|");
StringBuilder builder = new StringBuilder();
for(int a=0;a<var.length;a++)
builder.append(a);
// use builder.toString() when you want the cleaned album name.
To add the latest letter t to the end of the current string al:
b = al + t