读取JSON文件并显示其内容 [英] read JSON file and display its contents

查看:161
本文介绍了读取JSON文件并显示其内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Unix机器上有文件test.json,其数据如下所示

I have the file test.json on my Unix machine which has the data as below

{
  "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 jersey客户端,以读取test.json并显示如下输出

Now I'm writing the REST jersey client to read the test.json and show the output as below

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并显示其内容?

How do I read the test.json and display its contents?

推荐答案

对于recordId:

JSONObject jsonObject = (JSONObject) obj;
String name = (String) jsonObject.get("recordId");
System.out.println(name);

对于records:

 List<String> list = new ArrayList<String>();    
 JSONArray jsonList = jsonObject.getJSONArray("records");
  for (int i = 0; i < jsonList.length(); i++) 
    list.add(jsonList.getString(i));

您的records数据将位于list对象内部

Your records data will be inside list object

这篇关于读取JSON文件并显示其内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆