下面是我的代码,我试图检查id id = 101并获得名称= Pushkar与id = 101相关.但代码没有按预期工作.
import org.json.simple.JSONArray;
import org.json.JSONException;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class A6 {
public static void main(String[] args) throws ParseException, JSONException{
String out1="{\"Employee\":[{\"id\":\"101\",\"name\":\"Pushkar\",\"salary\":\"5000\"},{\"id\":\"102\",\"name\":\"Rahul\",\"salary\":\"4000\"},{\"id\":\"103\",\"name\":\"tanveer\",\"salary\":\"56678\"}]}";
//System.out.println(out1);
JSONParser parser=new JSONParser();
JSONObject obj=(JSONObject)parser.parse(out1);
//System.out.print(obj);
JSONArray jarr=(JSONArray)obj.get("Employee");
//System.out.print(jarr);
for (int i=0;i
{
JSONObject jobj=(JSONObject)jarr.get(i);
String ID1=(String)jobj.get("id");
System.out.println(ID1);
if(ID1!=null && out1.equals(ID1))
{
System.out.println("NAME"+jobj.get("name"));
}
}
}}