[英]How to fetch JSON object from Json array in REST API
I am new to REST API and handling JSON in our automation script. 我是REST API的新手,正在我们的自动化脚本中处理JSON。 I have an API whose response is JSONArray
ie, 我有一个API,其响应为JSONArray
即
[{"ProjectID":15,"ProjectName":" Securities"},{"ProjectID":16,"ProjectName":"PAS "}]
While automation, for verification I need to fetch the reponse. 在自动化过程中,为了进行验证,我需要获取响应。 I have tried the below one but not getting expected output 我已经尝试了以下方法,但未获得预期的输出
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
import org.json.*;
public class ProjectNameVerfication {
public static void main(String[] args) throws JSONException
{
try
{
URL url = new URL("http://17*.**.**.**:3000/api/******");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200)
{
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}
Scanner scan = new Scanner(url.openStream());
String str = new String();
while (scan.hasNext())
str += scan.nextLine();
scan.close();
System.out.println("str : " + str);
JSONObject obj = new JSONObject(str.substring(str.indexOf('{')));
System.out.println("obj : " +obj);
int ProjectID = obj.optInt("ProjectID");
String ProjectName = obj.getString("ProjectName");
System.out.println("ProjectID: " +ProjectID);
System.out.println("ProjectName: " +ProjectName);
JSONArray arr = obj.getJSONArray("ProjectID");
for (int i = 0; i < arr.length(); i++)
{
String post_id =arr.getJSONObject(i).getString("ProjectName");
}
conn.disconnect();
}
catch (MalformedURLException e) { e.printStackTrace();}
catch (IOException e) { e.printStackTrace(); }
}
}
Actual output is bellow: 实际输出如下:
str : [{"ProjectID":15,"ProjectName":" Securities"},{"ProjectID":16,"ProjectName":"PAS "}]
obj : {"ProjectName":" securities""ProjectID":15}
ProjectID: 15
ProjectName: Securities
Exception in thread "main" org.json.JSONException: JSONObject["ProjectID"] is not a JSONArray.
at org.json.JSONObject.getJSONArray(JSONObject.java:539)
at MyTest.ProjectNameVerfication.main(ProjectNameVerfication.java:60)
You want to get all ProjectName
,right? 您想获得所有ProjectName
,对吗?
look at your response data: 查看您的响应数据:
[{"ProjectID":15,"ProjectName":" Securities"},{"ProjectID":16,"ProjectName":"PAS "}]
it is already a JSONArray
string,you can parse it as JSONArray
,and get ProjectName
in foreach
它已经是一个JSONArray
字符串,您可以将其解析为JSONArray
,并在foreach
获取ProjectName
JSONArray jsonArray = new JSONArray("your str");
for (Object object : jsonArray) {
JSONObject jsonObject = (JSONObject)object;
System.out.println(jsonObject.toString());
}
Your response is an Array so you need JSONArray
instead of JSONObject
. 您的响应是一个Array,因此您需要JSONArray
而不是JSONObject
。
try {
JSONArray e = new JSONArray(str);
int l = e.length();
for (int x = 0; x < l; x++) {
JSONObject object1 = e.getJSONObject(x);
String projectID = object1.getString("ProjectID");
String projectName = object1.getString("ProjectName");
}
} catch (JSONException e) {
e.printStackTrace();
}
If you are using simple JSON you have to parse as follows. 如果您使用简单的JSON ,则必须按以下方式进行解析。
String str="[{\\"ProjectID\\":15,\\"ProjectName\\":\\" Securities\\"}, {\\"ProjectID\\":16,\\"ProjectName\\":\\"PAS \\"}]"; 字符串str =“ [{\\” ProjectID \\“:15,\\” ProjectName \\“:\\”证券\\“},{\\” ProjectID \\“:16,\\” ProjectName \\“:\\” PAS \\“}]”“ ;
JSONParser json=new JSONParser();
try {
JSONArray arr=(JSONArray)json.parse(str);
for (int i = 0; i < arr.size(); i++) {
JSONObject obj=(JSONObject)arr.get(i);
System.out.println("ProjectID: "+obj.get("ProjectID"));
System.out.println("ProjectName: "+obj.get("ProjectName"));
}
} catch (ParseException e) {
e.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.