将.json文件转换为JSONArray

时间:2011-10-05 00:38:01

标签: java json

我使用cURL以json文件(“twitter-feed.json”)的形式获取一些Twitter提要。我想将此json文件转换为JSONArray对象。我该怎么做?

我是Java和json的新手。非常欢迎您的建议。

FileInputStream infile = new FileInputStream("input/twitter-feed.json");

//解析JSON         JSONArray jsonArray = new JSONArray(string);

    // use
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject = jsonArray.getJSONObject(i);

        System.out.println(jsonObject.getString("id"));
        System.out.println(jsonObject.getString("text"));               
        System.out.println(jsonObject.getString("created_at"));     
    }

谢谢, PD。

3 个答案:

答案 0 :(得分:1)

您需要首先阅读该文件,将其转换为String,然后将其提供给JSONArray(我假设您使用的是JSON-Java Project。以下代码说明了如何阅读文件并将其设置为JSONArray


// read the source file, source comes from streaming API delimited by newline
// done by curl https://stream.twitter.com/1/statuses/sample.json?delimited=newline -utwitterUsername:twitterPasswd 
// > /Projects/StackOverflow/src/so7655570/twitter.json
FileReader f = new FileReader("/Projects/StackOverflow/src/so7655570/twitter.json");
BufferedReader br = new BufferedReader(f);

ArrayList jsonObjectArray = new ArrayList();
String currentJSONString  = "";

// read the file, since I ask for newline separation, it's easier for BufferedReader
// to separate each String
while( (currentJSONString = br.readLine()) != null ) {
    // create new JSONObject
    JSONObject currentObject = new JSONObject(currentJSONString);

    // there are more than one way to do this, right now  what I am doing is adding
    // each JSONObject to an ArrayList
    jsonObjectArray.add(currentObject);
}

for (int i = 0; i < jsonObjectArray.size(); i++) {
    JSONObject jsonObject = jsonObjectArray.get(i);

    // check if it has valid ID as delete won't have one
    // sample of JSON for delete : 
    // {"delete":{"status":{"user_id_str":"50269460","id_str":"121202089660661760","id":121202089660661760,"user_id":50269460}}}

    if(jsonObject.has("id")) {
        System.out.println(jsonObject.getInt("id"));
        System.out.println(jsonObject.getString("text"));               
        System.out.println(jsonObject.getString("created_at") + "\n");    
    }
}

步骤说明:

  • Stream API不提供有效的JSON作为整体,而是由delimited field指定的有效JSON。这就是为什么,你不能只是按原样解析整个结果。
  • 为了解析JSON,我使用delimited来使用newline,因为BufferedReader有一个我们可以直接用来获取每个JSONObject的方法readLine
  • 我从每一行获得每个有效的JSON后,创建JSONObject并将其添加到ArrayList
  • 然后我迭代JSONObject中的每个ArrayList并打印出结果。请注意,如果您想立即使用结果而不需要在以后使用它,您可以在while循环中进行处理,而不将它们存储在ArrayList中,从而将代码更改为:

// read the source file, source comes from streaming API
// done by curl https://stream.twitter.com/1/statuses/sample.json?delimited=newline -utwitterUsername:twitterPasswd 
// > /Projects/StackOverflow/src/so7655570/twitter.json
FileReader f = new FileReader("/Projects/StackOverflow/src/so7655570/twitter.json");
BufferedReader br = new BufferedReader(f);

String currentJSONString  = "";

// read the file, since I ask for newline separation, it's easier for BufferedReader
// to separate each String
while( (currentJSONString = br.readLine()) != null ) {
    // create new JSONObject
    JSONObject currentObject = new JSONObject(currentJSONString);

    // check if it has valid ID as delete status won't have one
    if(currentObject.has("id")) {
        System.out.println(currentObject.getInt("id"));
        System.out.println(currentObject.getString("text"));               
        System.out.println(currentObject.getString("created_at") + "\n");    
    }
}

答案 1 :(得分:0)

您可以尝试Gson

对于只是数组,您可以使用:

Gson gson = new Gson();

//(Deserialization)
int[] ints2 = gson.fromJson("[1,2,3,4,5]", int[].class);

要反序列化一个对象数组,您可以这样做:

Container container = new Gson().fromJson(json, Container.class);

作为shown here

答案 2 :(得分:0)

使用jackson库中的ObjectMapper类,如下所示:

//JSON from file to Object
Staff obj = mapper.readValue(new File("c:\\file.json"), Staff.class);

//JSON from URL to Object
Staff obj = mapper.readValue(new URL("http://mkyong.com/api/staff.json"), Staff.class);

//JSON from String to Object
Staff obj = mapper.readValue(jsonInString, Staff.class);