Reputation: 501
I have a json data listing all music scales and their keys.
this is how it looks like:
or the code view:
{
"allScalesJson": [
{
"scale": "A Major",
"keys": [
"b",
"a",
"e",
"d",
"gs",
"fs",
"cs"
]
},
{
"scale": "B Major",
"keys": [
"b",
"e",
"as",
"gs",
"fs",
"ds",
"cs"
]
},
{
"scale": "D Major",
"keys": [
"b",
"a",
"g",
"e",
"d",
"fs",
"cs"
]
}
]
}
I'm trying to create a method that will take keys
as parameter and return all scales that contains those keys.
Example based on above json data:
List<String> returned_scales = findScalesThatContainThisKeys(Arrays.asList("a","d"));
output:
A Major, D Major
Here it is:
private List<String> findScalesThatContainThisKeys(List<String> keys_array){
List<String> foundedScales = new ArrayList<>();
try {
JSONObject jsonObj = new JSONObject(loadJSONFromAsset());
JSONArray rootElement = jsonObj.getJSONArray("allScalesJson");
for (int i = 0; i < rootElement.length(); i++) {
JSONObject obj = rootElement.getJSONObject(i);
String scale = obj.getString("scale");
// Keys is json array
JSONArray genreArray = obj.getJSONArray("keys");
ArrayList<String> keys = new ArrayList<>();
for (int j = 0; j < genreArray.length(); j++) {
// TODO: 5/12/2018
}
}
}catch (Exception e){e.printStackTrace();}
return foundedScales;
}
Upvotes: 0
Views: 2268
Reputation: 6107
Use this code:
private List<String> findScales(List<String> keys_array){
List<String> foundedScales = new ArrayList<>();
try {
JSONObject jsonObj = new JSONObject(loadJSONFromAsset());
JSONArray rootElement = jsonObj.getJSONArray("allScalesJson");
for (int i = 0; i < rootElement.length(); i++) {
JSONObject obj = rootElement.getJSONObject(i);
String scale = obj.getString("scale");
// Keys is json array
JSONArray genreArray = obj.getJSONArray("keys");
ArrayList<String> keys = new ArrayList<>();
for (int j = 0; j < genreArray.length(); j++) {
// TODO: 5/12/2018
String str = genreArray.getString(j);
keys.add(str);
}
if(isContainsKeys(keys_array, keys)){
foundedScales.add(scale);
}
}
}catch (Exception e){e.printStackTrace();}
return foundedScales;
}
public boolean isContainsKeys(List<String> keys_array, List<String> keys ){
if (keys.size() == 0) {
return false;
}
for (String s : keys_array) {
if (!keys.contains(s)) {
return false;
}
}
return true;
}
This will ignore uppercase or lowercase issue. For that all string should be converted in one case (uppercase/lowercase)
Upvotes: 1
Reputation: 1127
Update your code like below
private List<String> findScales(List<String> keys_array){
List<String> foundedScales = new ArrayList<>();
try {
JSONObject jsonObj = new JSONObject(loadJSONFromAsset());
JSONArray rootElement = jsonObj.getJSONArray("allScalesJson");
for (int i = 0; i < rootElement.length(); i++) {
JSONObject obj = rootElement.getJSONObject(i);
String scale = obj.getString("scale"); // Keys is json array
JSONArray genreArray = obj.getJSONArray("keys");
ArrayList<String> keys = new ArrayList<>();
for (int j = 0; j < genreArray.length(); j++) {
if(keys_array.contains(genreArray.get(i)){
foundedScales.add(scale);
}
}
}
}catch (Exception e) {
e.printStackTrace();
}
return foundedScales;
}
Upvotes: 0
Reputation: 1195
Try this with for loops.
private List<String> findScales(List<String> keys_array){
List<String> foundedScales = new ArrayList<>();
try {
JSONObject jsonObj = new JSONObject(loadJSONFromAsset());
JSONArray rootElement = jsonObj.getJSONArray("allScalesJson");
ArrayList<String> scales_found = new ArrayList<>();
for (int i = 0; i < rootElement.length(); i++) {
JSONObject obj = rootElement.getJSONObject(i);
String scale = obj.getString("scale");
// Keys is json array
JSONArray genreArray = obj.getJSONArray("keys");
boolean all_found = true;
for(String key: keys_array){
boolean found_this_key = false;
for (int j = 0; j < genreArray.length(); j++) {
if(key.equals(genreArray.getString(j))){
found_this_key = true;
break;
}
}
if(!found_this_key){
all_found = false;
break;
}
}
if(all_found){
scales_found.add(scale);
}
}
return scales_found;
}catch (Exception e){e.printStackTrace();}
return foundedScales;
}
Upvotes: 1
Reputation: 495
try this, use Hashmap and get by key
public class MainActivity extends AppCompatActivity {
private RequestQueue mRequestQueue;
private StringRequest mStringRequest;
private String url = "url";
JSONArray jsonArray;
HashMap<String, String> meMap=new HashMap<String, String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRequestQueue = Volley.newRequestQueue(this);
//String Request initialized
mStringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
jsonArray = jsonObject.getJSONArray("allScalesJson");
for (int i = 0; i <jsonArray.length() ; i++) {
jsonObject = jsonArray.getJSONObject(i);
meMap.put(jsonObject.getString("scale"),jsonObject.getString("keys"));
}
Toast.makeText(MainActivity.this, meMap.toString(), Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, meMap.get("A Major"), Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
mRequestQueue.add(mStringRequest);
}
}
Upvotes: 0