2

I have a JSON array like this:

[
    {
        "name": "John",
        "city": "chicago",
        "age": "22"
    },
    {
        "name": "John",
        "city": "florida",
        "age": "35"
    },
    {
        "name": "Selena",
        "city": "vegas",
        "age": "18"
    },
    {
        "name": "Selena",
        "city": "Florida",
        "age": "19"
    }
]

I want to implement a function in Java which can take the JSON array, value and return a JSON String with all elements with passed value, Example:

public String returnSearch(JSONArray array, String searchValue){
    // let us say if the searchValue equals John, this method
    // has to return a JSON String containing all objects with
    // the name John 
}

Can anyone help me solve this issue? :)

Al-un
  • 3,102
  • 2
  • 21
  • 40
AbenaGood
  • 23
  • 1
  • 3

3 Answers3

3

You could try this:

    public String returnSearch(JSONArray array, String searchValue){
            JSONArray filtedArray = new JSONArray();
            for (int i = 0; i < array.length(); i++) {
                JSONObject obj= null;
                try {
                    obj = array.getJSONObject(i);
                    if(obj.getString("name").equals(searchValue))
                    {
                        filtedArray.put(obj);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
        }
        String result = filtedArray.toString();
        return result;
   }

Codes are self explanatory, so comments are omitted, hope it helpful.

navylover
  • 12,383
  • 5
  • 28
  • 41
0

You can create ArraList of HashMap to store you complete JSON data.

Here is example code.

public String MethodName(String json, String  search) {

    try {
        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();
        JSONArray jsonArray = new JSONArray(json);
        ArrayList<HashMap<String, String>> data = new ArrayList<>();
        for (int   i = 0 ; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);

            if (search.equalsIgnoreCase(jsonObject.getString("name"))) {
                array.put(jsonObject);
            }
        }
        return array.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Ali Ahmed
  • 2,130
  • 1
  • 13
  • 19
  • Thanks for Your answer, it's working fine but is there a way to return the values as [{"name":"john","city":"Florida","age":"35"},{"name":"john","city":"Chicago","age":"22"}] what I am getting now is [{name=john, city=Florida, age=35},{name=john, city=Chicago, age=22}] Thans :) – AbenaGood Oct 29 '18 at 12:13
0

Create a pojo class i.e User

class User{

String name;
String city;
String age;

// constructor with empty body
public User(){}

public User(String name,String city,String age){
  this.name = name;
  this.city = city;
  this.age = age;
}

// create getter and setter here
public String getName(){
  return this.name;
}

public String getCity(){
  return this.city;
}

public String getAge(){
  return this.age;
}
}

Now create your desired method with return type User Class

public User getUserInfo(String json, String  search) {

User user;
try {
    JSONArray jsonArray = new JSONArray(json);

    for (int   i = 0 ; i < jsonArray.length(); i++) {
        JSONObject jsonObject = jsonArray.getJSONObject(i);

        if (search.equalsIgnoreCase(jsonObject.getString("name"))) {
            String name = jsonObject.getString("name");
            String city = jsonObject.getString("city");
            String city = jsonObject.getString("age");

            user = new User(name, city, age);
        }                
    }
    return user;
} catch (Exception e) {
    e.printStackTrace();
}

Now call method and get data from User class object

User user = obj.getUserInfo(json, search);
String name = user.getName();
String city = user.getCity();
String age = user.getAge();
Omar
  • 901
  • 11
  • 14