0
我有我的Unix機器上的文件test.json它具有數據如下讀取JSON文件並顯示其內容
{
"recordId" :10,
"recordName" : "RECORDS",
"records" : [ {
"field1" : 111,
"titleField" : 1,
"titleIDMap" : null,
"titleId" : 500,
"titleStartDate" : "2013-12-22T00:00:00.000+0000",
"titleEndDate" : "2013-12-03T00:00:00.000+0000",
"languageId" : 20
}]
}
現在我正在寫的REST球衣客戶閱讀測試。 json並顯示如下輸出
public class RestWebServiceClient {
private static String BASE_URL="https://myUrl.com";
public static void main(String[] args) {
JSONParser parser = new JSONParser();
final String auth = "mfndfkndfkdnfkdnfdkfndkfndfkndkfndzxzxzxz==";
try {
Object obj = parser.parse(new FileReader("/home/user/test.json"));
JSONObject jsonObject = (JSONObject) obj;
final String postData = "dataTobePosted";
final String postResult = postJSONData(auth, BASE_URL+"/search",postData);
System.out.println("\n============postResponse============");
System.out.println(postResult);
JSONObject issueObj = new JSONObject(postResult);
}
catch (AuthenticationException e){
System.out.println("Username or Password wrong!");
e.printStackTrace();
} catch (JSONException e) {
System.out.println("Invalid JSON output");
e.printStackTrace();
} catch (ClientHandlerException e) {
System.out.println("Error invoking REST method");
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static String postJSONData(String auth, String url, String data) throws AuthenticationException, ClientHandlerException{
Client client = Client.create();
WebResource webResource = client.resource(url);
ClientResponse response = webResource.header("Authorization", "Basic " + auth).type("application/json")
.accept("application/json").post(ClientResponse.class, data);
int statusCode = response.getStatus();
if (statusCode == 401) {
throw new AuthenticationException("Invalid Username or Password");
}
return response.getEntity(String.class);
}
如何讀取test.json並顯示其內容?
@Kamlesh_Arya 感謝什麼生根粉打印記錄 –
@ AmitSharad..updated我回答 –
@Kamlesh_Arya postJSONData的返回是什麼 –