我是Android的初学者。我需要知道
1)如何将数组对象转换为JsonObject 。我需要这种格式
{"House_id":"1"
"Date":"12.12.2016"
[{"Name":"RickBald","email":"xyz@gmail.com","address":"homeaddress1"},{"Name":"EshaRan","email":"ghj@gmail.com","address":"homeaddress2"},{"Name":"Hansa Bella","email":"sas@gmail.com","address":"homeaddress3"}...]}
2) volley
中的请求应该是什么,如果我想发送这个` jsonArray 。
提前致谢
答案 0 :(得分:1)
使用您的响应字符串或Json字符串
JSONObject json = new JSONObject(strResponse);
Json.putString("Name","RickBald")
将所有值放在jsonObject中,如Json.putString(“Name”,“RickBald”);
它转换为inon jsonArray
JSONArray jsonarr = new JSONArray();
jsonarr.put(json);
<强>输出:强>
[{"Name":"RickBald","email":"xyz@gmail.com","address":"homeaddress1"},{"Name":"EshaRan","email":"ghj@gmail.com","address":"homeaddress2"},{"Name":"Hansa Bella","email":"sas@gmail.com","address":"homeaddress3"}...]
答案 1 :(得分:1)
首先声明数组:
String[] name ={"yousuf" , "Mohammed" , "Ali" , "Hamood" , "Alex"};
String[] emails = {"yousuf@dd" , "Mohammed@dd" , "Ali@dd" , "Hamood@dd" , "Alex@dd"};
任何包含数据的数组
然后你创建JSONArray
JSONArray array = new JSONArray();
然后你创建循环来向数组中添加对象:
for (int i =0; (i < name.length) && (i < emails.length) ; i++ ) {
JSONObject object = new JSONObject();
try {
object.put("name", name[i]);
object.put("email" , emails[i]);
array.put(object);
}catch (JSONException e) {
e.printStackTrace();
}
}
然后您可以在控制台中打印结果:
Log.d("Json is " , array.toString());
你会得到这样的结果:
[{"name":"yousuf","email":"yousuf@dd"},{"name":"Mohammed","email":"Mohammed@dd"},{"name":"Ali","email":"Ali@dd"},{"name":"Hamood","email":"Hamood@dd"},{"name":"Alex","email":"Alex@dd"}]
但你可以通过以下方式格式化: Json formatter and validator
这是整个代码:
public void getDataInJsonFormat (){
String[] name ={"yousuf" , "Mohammed" , "Ali" , "Hamood" , "Alex"};
String[] emails = {"yousuf@dd" , "Mohammed@dd" , "Ali@dd" , "Hamood@dd" , "Alex@dd"};
JSONArray array = new JSONArray();
for (int i =0; (i < name.length) && (i < emails.length) ; i++ ) {
JSONObject object = new JSONObject();
try {
object.put("name", name[i]);
object.put("email" , emails[i]);
array.put(object);
}catch (JSONException e) {
e.printStackTrace();
}
}
Log.d("Json is " , array.toString());
}
答案 2 :(得分:0)
您可以使用{{3}}来自Java模型的JSON表示转换器。
答案 3 :(得分:0)
您可以使用GSON lib和Jackson Object mapper
请参阅下面的GSON代码:
String convertedJson = getJSONString(DTO)
public String getJSONString(T dto){
return gson.toJson(dto);
}
T是以下方法getJSONString中的泛型类(DTO类)。
CallWebService: function() {
var webMethod = "http://localhost:30101/Api/MoviesAPI/GetMoviesList";
$.getJSON(webMethod, function (data) {
var myhtml = '';
$.each(data, function (i, item) {
debugger;
var imgs ="<img src='@Url.Content(item.pictures)' alt='Test' width='60' height='100' />";
myhtml += '<tr>';
myhtml += '<td>' + item.moviesID + '</td>';
myhtml += '<td>' + item.title + '</td>';
myhtml += '<td>' + item.language + '</td>';
myhtml += '<td>' + item.genre + '</td>';
myhtml += '<td>' + imgs + '</td>';
myhtml += '<td>' + item.documents + '</td>';
myhtml += '<td> <button type="submit" data-toggle="modal" data-target="#MyAddEditform" ';
myhtml += 'id="btnEdit" class="btnEdit btn btn-group-sm btn-primary">';
myhtml += ' Edit</button><button type="button" id="btnDelete" ';
myhtml += 'class="btnDelete btn btn-group-sm btn-danger"> Delete</button>' + '</td>' + '</tr>';
});
$("#myTable").append(myhtml);