简体   繁体   中英

How to convert JSON String to a String array?

I made a set of data and put into a JSON array and convert it to string so that I could store it into my sqlite database. When I take it out, it is a String and it has the form of a JSON array:

String temp = ["0", "1", "2", "3",.....]

Is there any easy way for me to make this into a String array, JSON array or I have to use the old fashion method(substring, split.etc)?

You can easily turn it back into a JSONArray by just constructing a new instance:

String jsonString;  //The string data you pulled out of the DB
JSONArray array = new JSONArray(jsonString);

If you need to go further, you could iterate over the array and turn it into a collection:

ArrayList<String> items = new ArrayList<String();
for(int i=0; i < array.length(); i++) {
    items.add(array.optString(i));
}

Try doing like this:

JSONArray jArray = new JSONArray(jsonString);

It will simply convert your json string (which is identified by [...] ) into JSONArray .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM