Colin747
Colin747

Reputation: 5023

JSON find value and return object with key

I have a program that searches through JSON objects to match values, when it matches a value I want it to return the object name along with the key associated with the matched value.

This is the code I currently have, it is matching the value and returning the object name as required, I need it to return the associated key with the matched value (I'm using the GSON library).

testHarnessSensor is the value to be matched, e.g. chair

ArrayList<String> currentActivity = new ArrayList<String>();
    int i=0;
    try {
      InputStream input = getClass().getResourceAsStream("/drink.json");
      JsonReader jsonReader = new JsonReader(new InputStreamReader(input));
      jsonReader.beginObject();
      while (jsonReader.hasNext()) {
        String nameRoot = jsonReader.nextName();
        if (nameRoot.equals("Activities")) {                   
          jsonReader.beginObject();                            
          while (jsonReader.hasNext()) {
            String activityName = jsonReader.nextName();      
            jsonReader.beginObject();                         
            while (jsonReader.hasNext()) {                    
              String n = jsonReader.nextName();
              n = jsonReader.nextString();                 
              if (testHarnessSensor.equals(n)) {
                currentActivity.add(activityName);
              }
            }
            jsonReader.endObject();
          }
          jsonReader.endObject();
        }
      }
      jsonReader.endObject();
      jsonReader.close();
    }
    catch (Exception e) {
      System.out.println(e);
    }

JSON file as requested:

{
  "Activities": {
  "Cold Drink": {
    "required": "kitchenDoor",
    "optional": "cup",
    "optional": "fridge",
    "required": "juice"
  },
  "Hot Drink": {
    "required": "kitchenDoor",
    "required": "cup",
    "required": "water",
    "required": "kettle",
    "optional": "sugar",
    "required": "microwave",
    "required": "tea/coffee"
  },
  "Hot Food": {
    "required": "kitchenDoor",
    "optional": "fridge",
    "optional": "plateCupboard",
    "required": "microwave",
    "optional": "cutlery",
    "optional": "chair"
  }
}}

Example:

If the value to match was chair the program would then print out Hot Food and optional.

Upvotes: 0

Views: 2019

Answers (1)

hemu
hemu

Reputation: 3261

Instead of taking currentActivity as ArrayList, use HashMap. So replace the first line of your code as follow:

Map<String, String> currentActivity = new HashMap<String, String>();

Now replace 3rd inner while loop, where you matching testHarnessSensor, with following while loop:

                while (jsonReader.hasNext()) {                    
                  String activityAttribute = jsonReader.nextName();
                  String n = jsonReader.nextString();                 
                  if (testHarnessSensor.equals(n)) {                 
                    currentActivity.put(activityName, activityAttribute);
                  }
                }

The map currentActivity will have the expected results. To print each key-value pair, use following code:

                for(String key : currentActivity.keySet()){
                    System.out.println(key + " : " + currentActivity.get(key));
                }

Upvotes: 1

Related Questions