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;
}
}