2012-05-01 123 views
2

數組我有這樣的迴應:閱讀JSON在JAVA

[ { "key": { "kind": "UserRecord", "id": 0, "name": "1" }, "firstName": "1", "lastName": "1", "homeLat": 0.0, "homeLon": 0.0, "workLat": 0.0, "workLon": 0.0, "currentLat": 10.0, "currentLon": 10.82, "timestamp": 1335735046606, "score": 0, "isStarred": false, "distance": 0.0 }, { "key": { "kind": "UserRecord", "id": 0, "name": "32423542324234324" }, "firstName": "Simone", "lastName": "Boscolo Berto", "homeLat": 0.0, "homeLon": 0.0, "workLat": 0.0, "workLon": 0.0, "currentLat": 55.786444, "currentLon": 12.515867, "timestamp": 1335884083696, "score": 0, "isStarred": false, "distance": 0.0 } ] 

,並從每個JSONObject的給我對象

private User getUserFromJson(JSONObject jsonUser) 

的方法我怎麼能重複在該列表一個JSONObjects?我應該使用JSONArrayobject?

回答

0

事情是這樣的:

final String incomingJSON ; 
final JSONArray objArray = new JSONArray(incomingJSON); 
for(int i = 0; i < objArray.length(); i++) { 
    final JSONObject obj = objArray.getJSONObject(i); 
    final User user = getUserFromJson(obj); 
} 
+0

給它的問題,因爲在創建JSONArray ... –

+0

的請告訴我它會給出確切的問題,請具體說明。 – Perception

+0

價值[email protected]類型java.lang.String不能轉換爲JSONArray –

0

是否使用了GSON API?用這個,你可以從JsonArray對象獲得一個迭代器。

Iterator<JsonElement> iterator() 
      Returns an iterator to navigate the elemetns of the array. 
1

這可能是容易GSON或傑克遜

GSON例如

Gson gson = new Gson(); 
User[] users = gson.fromJson(<json string>, User[].class); 

傑克遜例如

ObjectMapper mapper = new ObjectMapper(); 
Collection<User> users = 
    mapper.readValue(<json string>, `new TypeReference<Collection<User>>() {}); 

樣品 -

package com.test; 

import com.google.gson.Gson; 

public class GSonExample { 
    public static void main(String[] args) { 
     String json = "{\"name\":\"Duke\",\"address\":\"Menlo Park\",\"dateOfBirth\":\"Feb 1, 2000 12:00:00 AM\"}"; 

     Gson gson = new Gson(); 
     User student = gson.fromJson(json, User.class); 

     System.out.println("student.getName()  = " + student.getName()); 
     System.out.println("student.getAddress()  = " + student.getAddress()); 
     System.out.println("student.getDateOfBirth() = " + student.getDateOfBirth()); 
    } 
} 

public class User { 
    private String name; 
    private String address; 
    private String dateOfBirth; 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public String getAddress() { 
     return address; 
    } 
    public void setAddress(String address) { 
     this.address = address; 
    } 
    public String getDateOfBirth() { 
     return dateOfBirth; 
    } 
    public void setDateOfBirth(String dateOfBirth) { 
     this.dateOfBirth = dateOfBirth; 
    } 
} 

類路徑

<?xml version="1.0" encoding="UTF-8"?> 
<classpath> 
    <classpathentry kind="src" path="src"/> 
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> 
    <classpathentry kind="lib" path="lib/gson-2.1.jar"/> 
    <classpathentry kind="output" path="bin"/> 
</classpath> 
+0

我想Gson方法,因爲它看起來非常有用,但我發現在這個問題:「不能找到類'com.google.gson.Gson',從我的包中引用 順便說一句,我包括庫和編譯,它給這個問題只有運行時間 –

+0

它似乎令人驚訝,因爲如果你添加到建立路徑它不應該彈出此錯誤。您可以檢查此[線程](http://stackoverflow.com/questions/4961336/i-am-getting-java-lang-classnotfoundexception-com-google-gson-gson-error-even) –

+0

我已經看了那個線程,但沒有幫助,因爲我沒有一個Servlet \ JSP –