1

I have json array from server response like this.

[
    {
      "idApp" : "001"
      "AppName" : "Test App 1"
    },

    {
      "idApp" : "002"
      "AppName" : "Test App 2"
    },

    {
      "idApp" : "003"
      "AppName" : "Test App 3"
    },

    {
      "idApp" : "004"
      "AppName" : "Test App 4"
    }

]

i just want to know the position of this object in jsonarray programatically

{
          "idApp" : "003"
          "AppName" : "Test App 3"
}
1
  • 1
    use a for loop and and get the iteration number Commented Dec 7, 2015 at 10:46

4 Answers 4

1

This should work for you

for(int i = 0 ; i < arguments.length(); i++){
    if(arguments.getObject(i).get("idApp").asString().equals("003"))
    System.out.println("Found it : " + i);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Please pay attention to the indentation. :)
@YassinHajaj Oh, I copy-pasted it from the IDE ^^
It happens no problem :).
@YeHtetNyi You might aswell accept this answer as the correct one.
0

You forget , in each object in json string after each data like below :

{
          "idApp" : "003",
          "AppName" : "Test App 3"
}

By the way to get postion match 003 in idApp we can use Gson library http://mvnrepository.com/artifact/com.google.code.gson/gson/2.3.1

Add dependency in pom.xml :

<dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.3.1</version>
    </dependency>

Create modal class like your json object :

public class Modal {

    private String idApp;
    private String AppName;

    public String getIdApp() {
        return idApp;
    }

    public void setIdApp(String idApp) {
        this.idApp = idApp;
    }

    public String getAppName() {
        return AppName;
    }

    public void setAppName(String AppName) {
        this.AppName = AppName;
    }

}

Now convert json string to array of object and loop over array and find match object like below :

public class JsonUtils {

    public static void main(String[] args) {
        System.out.println("Position for 003 is = " + new JsonUtils().getPositionFromJsonString());
    }

    public int getPositionFromJsonString() {
        Gson gson = new Gson();
        String jsonString = "["
                + "    {"
                + "      \"idApp\" : \"001\","
                + "      \"AppName\" : \"Test App 1\""
                + "    },"
                + ""
                + "    {"
                + "      \"idApp\" : \"002\","
                + "      \"AppName\" : \"Test App 2\""
                + "    },"
                + ""
                + "    {"
                + "      \"idApp\" : \"003\","
                + "      \"AppName\" : \"Test App 3\""
                + "    },"
                + ""
                + "    {"
                + "      \"idApp\" : \"004\","
                + "      \"AppName\" : \"Test App 4\""
                + "    }"
                + ""
                + "]";
        Modal[] modals = gson.fromJson(jsonString, Modal[].class);
        int pos = -1;
        for (Modal m : modals) {
            pos++;
            if ("003".equalsIgnoreCase(m.getIdApp())) {
                return pos;
            }
        }
        return -1;
    }
}

Comments

0

You can do something like that to remove the specefic jsonObject from jsonArray.

//find the value in your jsonarray
      for (int i = 0; i < jsonarray.length; ++i)
      {         
      JSONObject rec =jsonarray.getJSONObject(i);
      //check the condition if the given id is associated with the object then remove it
        if(rec.getString("idApp").equals("your_passedId") 
        {
            jsonarray.remove(i)         
        }
     }

Comments

0

May be late, but it may help to someone

    int index = 0;
    while (index <  yourArray.length()) {
        JSONObject object = yourArray.optJSONObject(index);
        if (object != null) {
            if (/*your condition is true*/ true){
                yourArray.remove(index);
                index--;
            }
        }
        index++;
    }

Comments

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.