Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android how to convert json array to string array

Tags:

json

android

I have an app where I fetch data from server(json) in the form of array & by using the index i used in my app, like below.

JSONObject topobj = new JSONObject(page);
JSONObject innerobj = topobj.getJSONObject("restarutant");
JSONArray phone = innerobj.getJSONArray("phone");
textViewPhone.setText("Phone: " + phone.get(0).toString() + " ,"
                    + phone.get(1).toString());

for small size array I can get like this. But when array contains 'n' no of elements and dynamically i have to use this, at that time it required to convert into String Array. Can anybody tell me how I convert the json array to String array ? Thank you

like image 528
Jyosna Avatar asked Jun 15 '11 10:06

Jyosna


People also ask

Can we convert JSON array to string?

Stringify a JavaScript ArrayUse the JavaScript function JSON.stringify() to convert it into a string. const myJSON = JSON.stringify(arr); The result will be a string following the JSON notation.

How to convert JsonArray to String[] in Java?

Getting String array as output int size = exampleList. size(); String[] stringArray = exampleList. toArray(new String[size]); This will convert our JSON array into a String array.

How do you represent a JSON array of strings?

It can store string, number, boolean or object in JSON array. In JSON array, values must be separated by comma. The [ (square bracket) represents JSON array.

How to convert JSON object to String array in Java?

getJSONObject(i). getString("name"); value2 = jLocation. getJSONObject(i). getString("location"); } //Create Stting array String[ ] arr = {value1,value2};


3 Answers

I just did this yesterday! If you're willing to use a 3rd party library then you can use Google GSON, with the additional benefit of having more concise code.

String json = jsonArray.toString();
Type collectionType = new TypeToken<Collection<String>>(){}.getType();
Collection<String> strings = gson.fromJson(json, collectionType);

for (String element : strings)
{
    Log.d("TAG", "I'm doing stuff with: " + element);
}

You can find more examples in the user guide.

like image 90
Asim Ihsan Avatar answered Oct 27 '22 16:10

Asim Ihsan


Assume that you already have JSONArray jsonArray:

String[] stringArray = new stringArray[jsonArray.length()];
for(int i = 0, count = jsonArray.length(); i< count; i++)
{
    try {
        String jsonString = jsonArray.getString(i);
        stringArray[i] = jsonString.toString();
    }
    catch (JSONException e) {
        e.printStackTrace();
    }
}
like image 41
Mladen Rakonjac Avatar answered Oct 27 '22 17:10

Mladen Rakonjac


This should help you.

Edit:

Maybe this is what you need:

ArrayList<String> stringArray = new ArrayList<String>();
JSONArray jsonArray = new JSONArray();
for(int i = 0, count = jsonArray.length(); i< count; i++)
{
    try {
        JSONObject jsonObject = jsonArray.getJSONObject(i);
        stringArray.add(jsonObject.toString());
    }
    catch (JSONException e) {
        e.printStackTrace();
    }
}
like image 32
Pasha Avatar answered Oct 27 '22 15:10

Pasha