2011-10-05 134 views
1

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

我是Java和json的新手。您的建议是最受欢迎的。

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

//解析JSON JSONArray jsonArray =新JSONArray(字符串);

// 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。

回答

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

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");  
    } 
} 

步骤说明:

  • 流API不提供有效的JSON作为一个整体,而是由delimited field指定一个有效的。这就是为什么,你不能仅仅解析整个结果。
  • 为了解析JSON,我用的是delimited使用newline因为BufferedReader有一个方法readLine,我们可以直接用它来获取每个JSONObject的
  • 有一次,我从每一行获取每个有效的JSON,我创建JSONObject并添加它到ArrayList
  • 然后,我重复每个JSONObjectArrayList并打印出结果。请注意,如果您想立即使用该结果,并没有需要在以后使用它,你可以做加工本身在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");  
    } 
} 
+0

莫莫感谢您的建议。我试过了,我得到这个错误:线程“main”中的异常java.text.ParseException:“一个JSONArray必须以'['在字符1开头。”我的json文件以{开始并以}结束。必须有一种方法将其转换为数组.... – user979511

+0

这意味着输入不正确。我通过http://twitter.com/statuses/user_timeline/109531911.json这样的URL获得JSON输入,并在发布答案之前对其进行了测试。你能复制和粘贴input/twitter-feed.json的一些输入吗?它应该以字符'[' – momo

+0

'开头,另外,我可以获取twitter-feed.json的源代码吗?也许我从不同的来源获得不同的来源,它们有不同的JSON供稿 – momo

0

从杰克逊使用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);