我想创建一个像这样的json:
{
"blogs":{
"blog":{
"id":"","name":"","date":"","cant_post":0
},
"detailblog":[{
"id":"","name":"","date":0,"cant_post":0,
"creator":0,"theme":0
},
{
"id":"","name":"","date":0,"cant_post":0,
"creator":0,"theme":0
},
{
"id":"","name":"","date":0,"cant_post":0,
"creator":0,"theme":0
}]
}
}
从我的文本视图
name = txtName.getText().toString;
id = txtId.getText().toString;
等
但是我不知道如何,也不知道如何将{} []添加到json
有人可以解释我还是告诉我如何创建它?谢谢
答案 0 :(得分:0)
让我们通过自下而上的方法解决问题
1。创建detailblog
数组
JSONObject detailblog1 = new JSONObject();
try {
detailblog1.put("id","");
detailblog1.put("name","");
detailblog1.put("date",0);
detailblog1.put("cant_post",0);
detailblog1.put("creator",0);
detailblog1.put("theme",0);
} catch (JSONException e) {
e.printStackTrace();
}
JSONObject detailblog2 = new JSONObject();
try {
detailblog2.put("id","");
detailblog2.put("name","");
detailblog2.put("date",0);
detailblog2.put("cant_post",0);
detailblog2.put("creator",0);
detailblog2.put("theme",0);
} catch (JSONException e) {
e.printStackTrace();
}
JSONObject detailblog3 = new JSONObject();
try {
detailblog3.put("id","");
detailblog3.put("name","");
detailblog3.put("date",0);
detailblog3.put("cant_post",0);
detailblog3.put("creator",0);
detailblog3.put("theme",0);
} catch (JSONException e) {
e.printStackTrace();
}
JSONArray jsonArray = new JSONArray();
jsonArray.put(detailblog1);
jsonArray.put(detailblog2);
jsonArray.put(detailblog3);
2。完成blog
JSONObject
JSONObject blogs= new JSONObject();
try {
blogs.put("blog",blog);
blogs.put("detailblog",jsonArray);
} catch (JSONException e) {
e.printStackTrace();
}
3。完成blogs
JSONObject
JSONObject json = new JSONObject();
try {
json.put("blogs",blogs);
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("json",json.toString());