0

There is any data structure in java that i can work like a key value array from php or javascript?

$objectKeyTarget = "key2";


$array = array(
    "key1": {
        "id": 1,
        "name" "exemple 1"
    },
    "key2": {
        "id": 2,
        "name" "exemple 2"
    }
);

$dynamicObject = $array[$objectKeyTarget];
3
  • 2
    Map Commented Feb 3, 2020 at 23:26
  • You could try using Gson parsar for parsing json like array data and store it in java model list. Commented Feb 4, 2020 at 1:27
  • Did you take a look on developer.android.com/reference/java/util/HashMap? Commented Feb 7, 2020 at 20:39

2 Answers 2

2

Well you could simply use a Map and a List for that. I'd suggest something like this:

public class Entry {
  private int id;
  private String name;
  ...
}

Map<String, List<Entry>> entriesByKey = new HashMap<>();
entriesByKey.put("key1", new Entry(1,"exemple 1"));
entriesByKey.put("key2", new Entry(2,"exemple 2"))

Log.d(..., entriesByKey.get("key2"));

If you are trying to store a JSON-Respone: There is in-built JSONObject in Android, so use that one instead.

Sign up to request clarification or add additional context in comments.

Comments

-1

The simple way

not_array = {
  'Y':'YES',
  'N':'NO',
  'M': {'MY':'MAYBE YES','MN':'MAYBE NO'}
}
    
value  = 'Y';
result = not_array[value];
console.log(result);

value1  = 'M';
value2  = 'MN';
result1 = not_array[value1][value2];
console.log(result1);

1 Comment

Java is not the same as JavaScript

Your Answer

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