到對象我有一個WCF
Webservice
,其中發送一個數據模型,我得到這個Android
由JSon
(通過實體框架),任何方式, 我可以成功拿到JSON
這個代碼和存儲所有JSON
對象在JSONArray
在AsyncTas
類,並在:轉換JSONArray使用GSON fromJson方法
public class Consume extends AsyncTask<Void, Void, Void> {
InputStream inputStream = null;
String result = "";
private ArrayList<Contact> contacts = new ArrayList<Contact>();
@Override
protected Void doInBackground(Void... params) {
String URL = "http://x.x.x.x/MyWCF/Service1.svc/rest/getContact";
ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(URL);
post.setEntity(new UrlEncodedFormEntity(param));
HttpResponse httpResponse = httpClient.execute(post);
HttpEntity httpEntity = httpResponse.getEntity();
//post.setHeader("content-type", "application/json");
inputStream = httpEntity.getContent();
} catch (UnsupportedEncodingException e1) {
Log.e("UnsupportedEncoding", e1.toString());
e1.printStackTrace();
} catch (ClientProtocolException e2) {
Log.e("ClientProtocolException", e2.toString());
e2.printStackTrace();
} catch (IllegalStateException e3) {
Log.e("IllegalStateException", e3.toString());
e3.printStackTrace();
} catch (IOException e4) {
Log.e("IOException", e4.toString());
e4.printStackTrace();
}
try {
BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sBuilder = new StringBuilder();
String line = null;
while ((line = bReader.readLine()) != null) {
sBuilder.append(line + "\n");
}
inputStream.close();
result = sBuilder.toString();
} catch (Exception e) {
Log.e("StringBuilding", "Error converting result " + e.toString());
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
try {
JSONObject object = new JSONObject(result);
JSONArray jArray = object.getJSONArray("getContactResult"); //here i create the JsonArray of all JsonObjects
//Here is the solutions, We make a list of out Contact and make it as down
List<Contact> contacts;
Type listType = new TypeToken<List<Contact>>() {
}.getType();
contacts= new Gson().fromJson(String.valueOf(jArray), listType);
//And here solution is ended !
} catch (JSONException e) {
e.printStackTrace();
}
}
和我android
創建聯繫人class
,這段代碼:
public class Contact {
@SerializedName("name")
private String name;
@SerializedName("lastName")
private String lastName;
@SerializedName("phoneNumber")
private String phoneNumber;
@SerializedName("latitude")
private String latitude;
@SerializedName("longitude")
private String longitude;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return lastName;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLatitude() {
return latitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getLongitude() {
return longitude;
}
}
我用舊的方式解析這個JSONArray
! 通過這種方法:
ArrayList<Contact> setFields(JSONArray jsonArray) {
ArrayList<Contact> contacts = new ArrayList<Contact>();
for(int i=0; i<jsonArray.length(); i++) {
try {
Contact contact = new Contact();
JSONObject object = (JSONObject) jsonArray.get(i);
contact.setName(object.getString("name"));
contact.setLastName(object.getString("lastName"));
contact.setPhoneNumber(object.getString("phoneNumber"));
contact.setLatitude(object.getString("latitude"));
contact.setLongitude(object.getString("longitude"));
contacts.add(contact);
} catch (JSONException e) {
e.printStackTrace();
}
}
return contacts;
}
它的工作原理,但我不想來處理,並通過這種舊的方式,並想用GSON
代替解析JSONArray
,任何人可以幫我這個樣? 這是我JSONArray
和JSON
對象:
{
"getContactResult": [
{
"id": 2041,
"lastName": "xxxx",
"latitude": xxx,
"longitude": xxx,
"name": "xxxx",
"phoneNumber": "xxxx"
}
]
}
THX
Gson文檔還不清楚嗎?此外,也許你應該看看與Gson轉換器 –
Retrofit您可以嘗試使用[鏈接](http://stackoverflow.com/questions/8371274/how-to-parse-json-array-in-android-with-gson ) – asdcvf