0

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?

4
  • 3
    What language is this? Commented Feb 25, 2017 at 17:47
  • Plenty of ideas ... not many clues Commented Feb 25, 2017 at 17:49
  • a trick would be to use a regex like this : "sport":"([A-Za-z\s]*)" the capture group is exactly the sport word Commented Feb 25, 2017 at 17:51
  • Take a look at jq, it is a general parser/editor for json and can be utilized for all manner of json transformations. Commented Feb 25, 2017 at 17:55

1 Answer 1

1
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");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.