Java Simpleadapter(带子项的JSON)
我是Android开发的新手,我尝试了几天来让我的应用程序正常工作,但没有成功。 我使用的是Java Simpleadapter(带子项的JSON),java,android,Java,Android,我是Android开发的新手,我尝试了几天来让我的应用程序正常工作,但没有成功。 我使用的是AsyncTask和simpledapter,它返回数据,但是我的JSON有子项。它是一种注释的JSON,有时在注释中有该注释的答案。但是我不知道我是否在列表视图中执行列表视图。我真的迷路了,我找了很多,但我没有找到任何关于我的问题 遵循JSON的以下步骤: { COMENTARIOS: [ { comentarios_id: "1053939", noticias_id: "284901
AsyncTask
和simpledapter
,它返回数据,但是我的JSON
有子项。它是一种注释的JSON
,有时在注释中有该注释的答案。但是我不知道我是否在列表视图
中执行列表视图
。我真的迷路了,我找了很多,但我没有找到任何关于我的问题
遵循JSON的以下步骤:
{
COMENTARIOS: [
{
comentarios_id: "1053939",
noticias_id: "284901",
duelos_id: "0",
respostas_id: "0",
videos_id: "0",
nome: "ludi",
email: "email@email.com.br",
ip: "186.212.2.226",
comentario: "COMENTÁRIO",
data_cadastro: "2018-04-03 09:34:00",
status: "3",
gostei: "67",
naogostei: "143",
resp: [ ]
},
{
comentarios_id: "1053943",
noticias_id: "284901",
duelos_id: "0",
respostas_id: "0",
videos_id: "0",
nome: "Andre",
email: "email@email.com.br",
ip: "187.120.224.30",
comentario: "COMENTÁRIO",
data_cadastro: "2018-04-03 09:45:01",
status: "3",
gostei: "236",
naogostei: "114",
resp: [
{
comentarios_id: "1053945",
noticias_id: "284901",
duelos_id: "0",
respostas_id: "1053943",
videos_id: "0",
nome: "Usuário",
email: "email@email.com.br",
ip: "187.39.180.166",
comentario: "COMENTÁRIO",
data_cadastro: "2018-04-03 09:51:54",
status: "3",
gostei: "143",
naogostei: "219",
resp: [ ]
},
{
comentarios_id: "1053987",
noticias_id: "284901",
duelos_id: "0",
respostas_id: "1053943",
videos_id: "0",
nome: "Roberto, o Legítimo",
email: "email@email.com.br",
ip: "177.6.152.156",
comentario: "COMENTÁRIO",
data_cadastro: "2018-04-03 11:39:52",
status: "3",
gostei: "82",
naogostei: "124",
resp: [ ]
},
{
comentarios_id: "1054030",
noticias_id: "284901",
duelos_id: "0",
respostas_id: "1053943",
videos_id: "0",
nome: "xumbinho",
email: "email@email.com.br",
ip: "186.232.178.237",
comentario: "COMENTÁRIO",
data_cadastro: "2018-04-03 13:19:51",
status: "3",
gostei: "69",
naogostei: "116",
resp: [ ]
}
]
},
{
comentarios_id: "1053964",
noticias_id: "284901",
duelos_id: "0",
respostas_id: "0",
videos_id: "0",
nome: "Celso",
email: "email@email.com.br",
ip: "177.97.174.169",
comentario: "COMENTÁRIO",
data_cadastro: "2018-04-03 10:38:06",
status: "3",
gostei: "41",
naogostei: "116",
resp: [ ]
},
{
comentarios_id: "1054061",
noticias_id: "284901",
duelos_id: "0",
respostas_id: "0",
videos_id: "0",
nome: "Usuário",
email: "email@email.com.br",
ip: "181.223.181.27",
comentario: "COMENTÁRIO",
data_cadastro: "2018-04-03 14:23:10",
status: "3",
gostei: "60",
naogostei: "15",
resp: [
{
comentarios_id: "1054173",
noticias_id: "284901",
duelos_id: "0",
respostas_id: "1054061",
videos_id: "0",
nome: "bob",
email: "email@email.com.br",
ip: "92.59.121.38",
comentario: "COMENTÁRIO",
data_cadastro: "2018-04-03 17:19:32",
status: "3",
gostei: "5",
naogostei: "106",
resp: [ ]
},
{
comentarios_id: "1054485",
noticias_id: "284901",
duelos_id: "0",
respostas_id: "1054061",
videos_id: "0",
nome: "Usuário",
email: "email@email.com.br",
ip: "187.55.120.227",
comentario: "COMENTÁRIO",
data_cadastro: "2018-04-04 10:24:56",
status: "3",
gostei: "0",
naogostei: "0",
resp: [ ]
}
]
},
{
comentarios_id: "1054304",
noticias_id: "284901",
duelos_id: "0",
respostas_id: "0",
videos_id: "0",
nome: "Jorge",
email: "email@email.com.br",
ip: "189.74.113.127",
comentario: "COMENTÁRIO",
data_cadastro: "2018-04-03 21:00:03",
status: "3",
gostei: "1",
naogostei: "0",
resp: [ ]
},
{
comentarios_id: "1054382",
noticias_id: "284901",
duelos_id: "0",
respostas_id: "0",
videos_id: "0",
nome: "Pé na cova",
email: "email@email.com",
ip: "168.194.160.137",
comentario: "COMENTÁRIO",
data_cadastro: "2018-04-03 23:45:29",
status: "3",
gostei: "1",
naogostei: "0",
resp: [ ]
}
]
}
try {
JSONObject jsonObject1 = new JSONObject("{\n" +
"\t\"COMENTARIOS\": [{\n" +
"\t\t\"comentarios_id\": \"1053939\",\n" +
"\t\t\"noticias_id\": \"284901\",\n" +
"\t\t\"duelos_id\": \"0\",\n" +
"\t\t\"respostas_id\": \"0\",\n" +
"\t\t\"videos_id\": \"0\",\n" +
"\t\t\"nome\": \"ludi\",\n" +
"\t\t\"email\": \"email@email.com.br\",\n" +
"\t\t\"ip\": \"186.212.2.226\",\n" +
"\t\t\"comentario\": \"COMENTÁRIO\",\n" +
"\t\t\"data_cadastro\": \"2018-04-03 09:34:00\",\n" +
"\t\t\"status\": \"3\",\n" +
"\t\t\"gostei\": \"67\",\n" +
"\t\t\"naogostei\": \"143\",\n" +
"\t\t\"resp\": []\n" +
"\t}]\n" +
"}");
JSONArray jsonArray = jsonObject1.getJSONArray("COMENTARIOS");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String comentarios_id = jsonObject.get("noticias_id").toString();
System.out.println("----comentarios_id---" + comentarios_id);
}
} catch (JSONException e) {
e.printStackTrace();
System.out.println("----comentarios_id---exc--" + e);
}
在受保护的Void doInBackground(Void…arg0)
内的AsyncTask中,我通过以下方式进行JSON调用:
JSONArray comentarios = jsonObj.getJSONArray("COMENTARIOS");
for (int i = 0; i < comentarios.length(); i++) {
JSONObject c = comentarios.getJSONObject(i);
String nome = c.getString("nome");
String comentario = c.getString("comentario");
HashMap<String, String> comentariotxt = new HashMap<>();
comentariotxt.put("nome", nome);
comentariotxt.put("comentario", comentario);
JSONArray resp = c.getJSONArray("resp");
if(resp.length() > 0){
for (int j = 0; j < resp.length(); j++){
JSONObject d = resp.getJSONObject(j);
String respostaNome = d.getString("nome");
String respostaComentario = d.getString("comentario");
Log.d("respostaNome",respostaNome);
HashMap<String, String> respostatxt = new HashMap<>();
respostatxt.put("respostaNome", respostaNome);
respostatxt.put("respostaComentario", respostaComentario);
comentarioList.add(respostatxt);
}
}
comentarioList.add(comentariotxt);
}
JSONArray-comentarios=jsonObj.getJSONArray(“comentarios”);
对于(int i=0;i0){
对于(int j=0;j
但在这里它已经把一切都搞砸了。嗯,我需要两样东西。让他们将评论和答案按顺序排列,气球响应的外观将与评论气球不同。目前我只做了一个评论列表视图,但我也不确定是否需要另一个列表视图来获得答案。我是说。。。我很迷路!有人帮我吗
非常感谢你们 我试图用单个对象修改您的json 请检查以下代码以解析此json:
{
COMENTARIOS: [
{
comentarios_id: "1053939",
noticias_id: "284901",
duelos_id: "0",
respostas_id: "0",
videos_id: "0",
nome: "ludi",
email: "email@email.com.br",
ip: "186.212.2.226",
comentario: "COMENTÁRIO",
data_cadastro: "2018-04-03 09:34:00",
status: "3",
gostei: "67",
naogostei: "143",
resp: [ ]
},
{
comentarios_id: "1053943",
noticias_id: "284901",
duelos_id: "0",
respostas_id: "0",
videos_id: "0",
nome: "Andre",
email: "email@email.com.br",
ip: "187.120.224.30",
comentario: "COMENTÁRIO",
data_cadastro: "2018-04-03 09:45:01",
status: "3",
gostei: "236",
naogostei: "114",
resp: [
{
comentarios_id: "1053945",
noticias_id: "284901",
duelos_id: "0",
respostas_id: "1053943",
videos_id: "0",
nome: "Usuário",
email: "email@email.com.br",
ip: "187.39.180.166",
comentario: "COMENTÁRIO",
data_cadastro: "2018-04-03 09:51:54",
status: "3",
gostei: "143",
naogostei: "219",
resp: [ ]
},
{
comentarios_id: "1053987",
noticias_id: "284901",
duelos_id: "0",
respostas_id: "1053943",
videos_id: "0",
nome: "Roberto, o Legítimo",
email: "email@email.com.br",
ip: "177.6.152.156",
comentario: "COMENTÁRIO",
data_cadastro: "2018-04-03 11:39:52",
status: "3",
gostei: "82",
naogostei: "124",
resp: [ ]
},
{
comentarios_id: "1054030",
noticias_id: "284901",
duelos_id: "0",
respostas_id: "1053943",
videos_id: "0",
nome: "xumbinho",
email: "email@email.com.br",
ip: "186.232.178.237",
comentario: "COMENTÁRIO",
data_cadastro: "2018-04-03 13:19:51",
status: "3",
gostei: "69",
naogostei: "116",
resp: [ ]
}
]
},
{
comentarios_id: "1053964",
noticias_id: "284901",
duelos_id: "0",
respostas_id: "0",
videos_id: "0",
nome: "Celso",
email: "email@email.com.br",
ip: "177.97.174.169",
comentario: "COMENTÁRIO",
data_cadastro: "2018-04-03 10:38:06",
status: "3",
gostei: "41",
naogostei: "116",
resp: [ ]
},
{
comentarios_id: "1054061",
noticias_id: "284901",
duelos_id: "0",
respostas_id: "0",
videos_id: "0",
nome: "Usuário",
email: "email@email.com.br",
ip: "181.223.181.27",
comentario: "COMENTÁRIO",
data_cadastro: "2018-04-03 14:23:10",
status: "3",
gostei: "60",
naogostei: "15",
resp: [
{
comentarios_id: "1054173",
noticias_id: "284901",
duelos_id: "0",
respostas_id: "1054061",
videos_id: "0",
nome: "bob",
email: "email@email.com.br",
ip: "92.59.121.38",
comentario: "COMENTÁRIO",
data_cadastro: "2018-04-03 17:19:32",
status: "3",
gostei: "5",
naogostei: "106",
resp: [ ]
},
{
comentarios_id: "1054485",
noticias_id: "284901",
duelos_id: "0",
respostas_id: "1054061",
videos_id: "0",
nome: "Usuário",
email: "email@email.com.br",
ip: "187.55.120.227",
comentario: "COMENTÁRIO",
data_cadastro: "2018-04-04 10:24:56",
status: "3",
gostei: "0",
naogostei: "0",
resp: [ ]
}
]
},
{
comentarios_id: "1054304",
noticias_id: "284901",
duelos_id: "0",
respostas_id: "0",
videos_id: "0",
nome: "Jorge",
email: "email@email.com.br",
ip: "189.74.113.127",
comentario: "COMENTÁRIO",
data_cadastro: "2018-04-03 21:00:03",
status: "3",
gostei: "1",
naogostei: "0",
resp: [ ]
},
{
comentarios_id: "1054382",
noticias_id: "284901",
duelos_id: "0",
respostas_id: "0",
videos_id: "0",
nome: "Pé na cova",
email: "email@email.com",
ip: "168.194.160.137",
comentario: "COMENTÁRIO",
data_cadastro: "2018-04-03 23:45:29",
status: "3",
gostei: "1",
naogostei: "0",
resp: [ ]
}
]
}
try {
JSONObject jsonObject1 = new JSONObject("{\n" +
"\t\"COMENTARIOS\": [{\n" +
"\t\t\"comentarios_id\": \"1053939\",\n" +
"\t\t\"noticias_id\": \"284901\",\n" +
"\t\t\"duelos_id\": \"0\",\n" +
"\t\t\"respostas_id\": \"0\",\n" +
"\t\t\"videos_id\": \"0\",\n" +
"\t\t\"nome\": \"ludi\",\n" +
"\t\t\"email\": \"email@email.com.br\",\n" +
"\t\t\"ip\": \"186.212.2.226\",\n" +
"\t\t\"comentario\": \"COMENTÁRIO\",\n" +
"\t\t\"data_cadastro\": \"2018-04-03 09:34:00\",\n" +
"\t\t\"status\": \"3\",\n" +
"\t\t\"gostei\": \"67\",\n" +
"\t\t\"naogostei\": \"143\",\n" +
"\t\t\"resp\": []\n" +
"\t}]\n" +
"}");
JSONArray jsonArray = jsonObject1.getJSONArray("COMENTARIOS");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String comentarios_id = jsonObject.get("noticias_id").toString();
System.out.println("----comentarios_id---" + comentarios_id);
}
} catch (JSONException e) {
e.printStackTrace();
System.out.println("----comentarios_id---exc--" + e);
}
试试看{
JSONObject JSONObject 1=新的JSONObject(“{\n”+
“\t\“COMENTARIOS\”:[{\n”+
\t\t\“comentarios\u id\:\“1053939\”,\n+
\t\t\“通知id\:\“284901\”,\n+
\t\t“duelos\u id\:“0\,\n”+
\t\t\“respostas\u id\:\“0\,\n”+
\t\t\“视频\u id\:\“0\,\n”+
\t\t\“nome\:\“ludi\”,\n+
“\t\t\”电子邮件\“:\”email@email.com.br\“,\n”+
“\t\t\'ip\':\'186.212.2.226\',\n”+
“\t\t\'comentario\':\'Comentrio\',\n”+
“\t\t\'data\u cadastro\”:\“2018-04-03 09:34:00\”,\n”+
“\t\t\”状态\“:\”3\“,\n”+
“\t\t\'gostei\':\'67\',\n”+
“\t\t\'naogostei\':\'143\',\n”+
“\t\t\“resp\”:[]\n”+
“\t}]\n”+
"}");
JSONArray JSONArray=jsonObject1.getJSONArray(“COMENTARIOS”);
for(int i=0;i
注意:
在我传递静态json的json对象中,您需要传递自己的字符串,所有json都存储在这个字符串中。显示正确的json。这个json无效,因为我正在json验证器上尝试它。非常感谢您的回复!但是我的问题是在布局中显示这个。我不知道如何在listview中显示评论,在有答案的评论下面显示评论的答案。