1

I want to get the object of jsonarray if the attribute values are matched from jsonarray. Below code is good to get the attribute value by checking the list one by one.

JSONParser parser1 = new JSONParser();
JSONArray array = (JSONArray)parser1.parse("[{\"user_id\": 1,\"Name\":\"Andrew\"}, {\"user_i\": 1}, {\"user\": 1}]");

 for (int i =0; i<array.size();i++){
           System.out.println(((JSONObject)array.get(i)).get("Name"));
  }

However, I want to get the index (list) i.e. {\"user_id\": 1,\"Name\":\"Andrew\"} when attribute values matches. something like

System.out.println(((JSONObject)array.get("Name")).get(i));

Can anyone please suggest me how to achieve this?

5
  • "i want to get the index (list) ie {\"user_id\": 1,\"Name\":\"Andrew\"}" An index is a number. {\"user_id\": 1,\"Name\":\"Andrew\"} isn't a number. What is it you're really looking for...? Commented Mar 31, 2021 at 6:17
  • I'm not quite sure what challenge you're facing but why don't you just store the result of (JSONObject)array.get(i) and call get("Name") etc. on that? If the values match your requirements you found the json object you need and can keep working with it. Commented Mar 31, 2021 at 6:19
  • Does this answer your question? Find index of element in a JSONArray Commented Mar 31, 2021 at 6:20
  • @Thomas, Due to performance issue , beacuse , i need to check a long list every time Commented Mar 31, 2021 at 7:55
  • I don't quite get your concerns about performance here. Any code that needs to find elements (or a single element) in a list that matches some properties would have to iterate over that list so there's nothing to be gained here. If the data doesn't change but you need to look for objects like this multiple times you might consider building maps instead (you might need a map per property or property combination). Commented Mar 31, 2021 at 8:08

1 Answer 1

0

If I'm understanding you properly, you can do it with a for loop and if to get the index of the object in the array:

int index = -1;
for (int i = 0, len = array.size(); i < len; ++i) {
    String name = (JSONObject)array.get(i)).get("Name");
    if (name.equals("Andrew")) {
        index = i;
        break;
    }
}

There's probably some streams equivalent to that as well.

Or if you want the object, not its index:

JSONObject result = null;
for (int i = 0, len = array.size(); i < len; ++i) {
    JSONObject obj = (JSONObject)array.get(i);
    if (obj.get("Name").equals("Andrew")) {
        result = obj;
        break;
    }
}

or with an enhanced for loop:

JSONObject result = null;
for (Object obj : array) {
    JSONObject jsonObj = (JSONObject)obj;
    if (jsonObj.get("Name").equals("Andrew")) {
        result = jsonObj;
        break;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

@Crowder, my mistake if i am not able explain properly. What i mean i want to get the whole list '{\"user_id\": 1,\"Name\":\"Andrew\"}' which list is having name as "Andrew"
@user3302083 I'm afraid I don't understand what you mean by "list." If you want the object that contains "Name": "Andrew", that's the second and third examples above.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.