2011-10-05 107 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);