2016-03-30 16 views
0

ArrayList<Orto> listOrto json'umu sunucuya göndermek istiyorum. İşte benim kodumjsonArray'ı GsonBuilder veya Gson oluştur

String url = serverUrl + "addOrto1212"; //tırnak içi addOrto olacak 
HttpClient client = new DefaultHttpClient(); 
HttpConnectionParams.setConnectionTimeout(client.getParams(), 100000); 
try{ 
JSONObject json = new JSONObject();  
Gson gson = new GsonBuilder().create(); 
JsonArray myCustomArray = gson.toJsonTree(listOrto).getAsJsonArray(); 
json.put("list",myCustomArray); 
    Log.d("******************json",json.toString()); 
HttpPost post = new HttpPost(url); 
StringEntity se = new StringEntity(json.toString()); 
post.setEntity(se); 
client.execute(post); 
}catch(Exception e){ 
// 
} 

Bakın Logcat;

03-30 09:24:18.655 21698-22331/com.emato.microorthodontist D/******************json: {"macid":"C9:3F:C1:B1:6F:1F","list":"[{\"date\":\"2016\/03\/30\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":31,\"dest\":0,\"melvalue\":11},{\"date\":\"2016\/03\/29\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":16,\"dest\":0,\"melvalue\":95},{\"date\":\"2016\/03\/28\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":17,\"dest\":0,\"melvalue\":0},{\"date\":\"2016\/03\/27\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":18,\"dest\":0,\"melvalue\":0},{\"date\":\"2016\/03\/26\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":19,\"dest\":0,\"melvalue\":0},{\"date\":\"2016\/03\/25\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":20,\"dest\":0,\"melvalue\":0},{\"date\":\"2016\/03\/24\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":21,\"dest\":0,\"melvalue\":0},{\"date\":\"2016\/03\/23\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":22,\"dest\":0,\"melvalue\":0},{\"date\":\"2016\/03\/22\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":23,\"dest\":0,\"melvalue\":0},{\"date\":\"2016\/03\/21\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":24,\"dest\":0,\"melvalue\":0},{\"date\":\"2016\/03\/20\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":25,\"dest\":0,\"melvalue\":0},{\"date\":\"2016\/03\/19\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":26,\"dest\":0,\"melvalue\":0},{\"date\":\"2016\/03\/18\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":27,\"dest\":0,\"melvalue\":0},{\"date\":\"2016\/03\/17\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":28,\"dest\":0,\"melvalue\":0},{\"date\":\"2016\/03\/16\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":29,\"dest\":0,\"melvalue\":0},{\"date\":\"2016\/03\/15\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":30,\"dest\":0,\"melvalue\":0}]"} 

"list" bana bir listorto'yu döndürür ancak jsonArray'ı istiyorum. Benim hatadır Nerede yardım lütfen ..

+0

GSON json formatında veriler bir dizge olarak döndürülür dönüştürün. Bunu elle yapmanız gerekiyor –

cevap

0

Eğer, eski ile çalışıyoruz verileri temsil etmek POJO sınıf oluşturabileceğini:

populate:

public class MyData { 
    private String macid; 
    private ArrayList<MyListItem> myList= new ArrayList<MyListItem>(); 

    // create setters and getters 
} 

public class MyListItem { 

    private String date; 
    private String mac; 
    private Integer id; 
    private Integer dest; 
    private Integer melvalue; 

    // create setters and getters 
} 

sonra ve JSON'dan dönüştürmek için GSON kullanmak

: veri

MyData mydata = new MyData(); 
mydata.setMacid("some:mac:id"); 
MyListItem item = new MyListItem(); 
//item .set ... set other values for the item 
mydata.getMyList().add(item);//add as many items as you want 

GSON bir örneğini oluşturun ... nesneleri Json için

Gson gson = new Gson(); 

1-: JSON'dan

//from object to json string 
String mydataAsJson = gson.toJson(mydata, MyData.class); 

2-:

//from json string to object 
String jsonStr = getJsonFromServer(); 
MyData mydata2 = gson.fromJson(jsonStr, MyData.class);