user7312508
user7312508

Reputation:

how do convert json string to a string

I have the json string from MySQL database:

{"teams":[{"sport":"NFL"},{"sport":"NBA"},{"sport":"NCAA Football"},{"sport":"NCAA Men Basketball"}],"success":1}

And I need to convert this string to the following format:

 final String sports[] = {"NFL", "NBA", "NCAA Football", "NCAA Men Basketball"};

Any ideas how to do this?

Upvotes: 0

Views: 50

Answers (1)

mahiraj2709
mahiraj2709

Reputation: 71

import org.json.*;
String jsonString = yourJsonStringFromDatabase;
JSONObject obj = new JSONObject(jsonString);

JSONArray team = obj.getJSONArray("teams");
String[] sports = new String[team.length());
for (int i = 0; i < team.length(); i++)
{
    sports[i] = arr.getJSONObject(i).getString("sport");
}

Upvotes: 1

Related Questions