I want to ask a question about converting a jsonArray
to a StringArray
on Android
. Here is my code to get jsonArray
from server.
try {
DefaultHttpClient defaultClient = new DefaultHttpClient();
HttpGet httpGetRequest = new HttpGet("http://server/android/listdir.php");
HttpResponse httpResponse = defaultClient.execute(httpGetRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(),"UTF-8"));
String json = reader.readLine();
//JSONObject jsonObject = new JSONObject(json);
JSONArray jsonArray = new JSONArray(json);
Log.d("", json);
//Toast.makeText(getApplicationContext(), json, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
And this is the JSON
.
[
{"name": "IMG_20130403_140457.jpg"},
{"name":"IMG_20130403_145006.jpg"},
{"name":"IMG_20130403_145112.jpg"},
{"name":"IMG_20130404_085559.jpg"},
{"name":"IMG_20130404_113700.jpg"},
{"name":"IMG_20130404_113713.jpg"},
{"name":"IMG_20130404_135706.jpg"},
{"name":"IMG_20130404_161501.jpg"},
{"name":"IMG_20130405_082413.jpg"},
{"name":"IMG_20130405_104212.jpg"},
{"name":"IMG_20130405_160524.jpg"},
{"name":"IMG_20130408_082456.jpg"},
{"name":"test.jpg"}
]
How can I convert jsonArray that I've got to StringArray so I can get StringArray like this:
array = {"IMG_20130403_140457.jpg","IMG_20130403_145006.jpg",........,"test.jpg"};
Thank you for your help :)
Convert JSON to Array Using `json. The parse() function takes the argument of the JSON source and converts it to the JSON format, because most of the time when you fetch the data from the server the format of the response is the string. Make sure that it has a string value coming from a server or the local source.
We can also add a JSONArray to JSONObject. We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject using the put() method.
Take a look at this tutorial. Also you can parse above json like :
JSONArray arr = new JSONArray(yourJSONresponse);
List<String> list = new ArrayList<String>();
for(int i = 0; i < arr.length(); i++){
list.add(arr.getJSONObject(i).getString("name"));
}
Simplest and correct code is:
public static String[] toStringArray(JSONArray array) {
if(array==null)
return new String[0];
String[] arr=new String[array.length()];
for(int i=0; i<arr.length; i++) {
arr[i]=array.optString(i);
}
return arr;
}
Using List<String>
is not a good idea, as you know the length of the array.
Observe that it uses arr.length
in for
condition to avoid calling a method, i.e. array.length()
, on each loop.
public static String[] getStringArray(JSONArray jsonArray) {
String[] stringArray = null;
if (jsonArray != null) {
int length = jsonArray.length();
stringArray = new String[length];
for (int i = 0; i < length; i++) {
stringArray[i] = jsonArray.optString(i);
}
}
return stringArray;
}
TERRIBLE TERRIBLE TERRIBLE hack:
String[] arr = jsonArray.toString().replace("},{", " ,").split(" ");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With