【问题标题】:Parsing JSON in Android, not getting value在Android中解析JSON,没有获得价值
【发布时间】:2016-09-20 08:48:30
【问题描述】:

我能够解析我需要的所有内容,除了 field_exercis_arc 中的 target_id's
我得到了nid,标题和正文。不确定如何获取 field_exercis_arc 中的 id。


JSON

[{
"nid": "26",
"title": "Question test",
"body": "xcvxcv",
"field_exercis_arc": ["25","27"]
}]

代码

String finalJson = buffer.toString();
            JSONArray parentArray = new JSONArray(finalJson);
            List<ExerciseModel> exerciseModelList = new ArrayList<>();

            for(int i=0; i<parentArray.length(); i++){
                JSONObject finalObject = parentArray.getJSONObject(i);

                title_exi = finalObject.getString("title");
                text_exi = finalObject.getString("body");
                //This part is working.
                ExerciseModel exerciseModel = new ExerciseModel();
                exerciseModel.setTitle(finalObject.getString("title"));
                exerciseModel.setNid(finalObject.getInt("nid"));
                exerciseModel.setBody(finalObject.getString("body"));

                //Problem with this part, not getting the target_id's.
                List<ExerciseModel.Exer> exerList = new ArrayList<>();
                for(int j=0; j<finalObject.getJSONArray("field_exercis_arc").length(); j++){
                    ExerciseModel.Exer exercis = new ExerciseModel.Exer();
                    exercis.setTarget_id(finalObject.getJSONArray("field_exercis_arc").getJSONObject(j).getString("target_id"));
                    exerList.add(exercis);
                }
                exerciseModel.setExerList(exerList);
                exerciseModelList.add(exerciseModel);

                mDB.saveRecordEX(exerciseModel);
            }

field_exercis_arc 和 target_id 字段的模型

private List<Exer> exerList;
public List<Exer> getExerList() {
    return exerList;
}

public void setExerList(List<Exer> exerList) {
    this.exerList = exerList;
}

public static class Exer{
    private String target_id;
    public String getTarget_id() {
        return target_id;
    }
    public void setTarget_id(String target_id) {
        this.target_id = target_id;
    }
}

提前致谢

【问题讨论】:

  • 您的 JSOn 未验证请检查

标签: java android arrays json arraylist


【解决方案1】:

我建议您使用 GSON 库从 JSON 中获取结果。为此,您将需要 Java 类才能将结果解析为对象。为此,您可以使用 JSON 到 Java 类转换 here

你的示例类是:

public class Und
{
  private String value;

  public String getValue() { return this.value; }

  public void setValue(String value) { this.value = value; }
}

public class Body
{
  private ArrayList<Und> und;

  public ArrayList<Und> getUnd() { return this.und; }

  public void setUnd(ArrayList<Und> und) { this.und = und; }
}

public class Und2
{
  private String target_id;

  public String getTargetId() { return this.target_id; }

  public void setTargetId(String target_id) { this.target_id = target_id; }
}

public class FieldExercisArc
{
  private ArrayList<Und2> und;

  public ArrayList<Und2> getUnd() { return this.und; }

  public void setUnd(ArrayList<Und2> und) { this.und = und; }
}

public class RootObject
{
  private String vid;

  public String getVid() { return this.vid; }

  public void setVid(String vid) { this.vid = vid; }

  private String uid;

  public String getUid() { return this.uid; }

  public void setUid(String uid) { this.uid = uid; }

  private String title;

  public String getTitle() { return this.title; }

  public void setTitle(String title) { this.title = title; }

  private Body body;

  public Body getBody() { return this.body; }

  public void setBody(Body body) { this.body = body; }

  private FieldExercisArc field_exercis_arc;

  public FieldExercisArc getFieldExercisArc() { return this.field_exercis_arc; }

  public void setFieldExercisArc(FieldExercisArc field_exercis_arc) { this.field_exercis_arc = field_exercis_arc; }

  private String cid;

  public String getCid() { return this.cid; }

  public void setCid(String cid) { this.cid = cid; }

  private String last_comment_timestamp;

  public String getLastCommentTimestamp() { return this.last_comment_timestamp; }

  public void setLastCommentTimestamp(String last_comment_timestamp) { this.last_comment_timestamp = last_comment_timestamp; }
}

您可以将结果转换为 RootObject。狐狸例子:

 String json = "{\"vid\": \"26\",\"uid\": \"1\",\"title\": \"Question test\",\"body\": {\"und\": [{\"value\": \"xcvxcv\"}]},\"field_exercis_arc\": {\"und\": [{\"target_id\": \"25\"},{\"target_id\":\"27\"}]},\"cid\": \"0\",\"last_comment_timestamp\": \"1472217577\"}";
 RootObject object = new Gson().fromJson(json, RootObject.class);
 System.out.println("Title is: "+object.getTitle() ); 

结果是:

Title is: Question test

在此之后,您可以使用您的对象从您的 JSON 中获取任何值。

您还应该知道您的 JSON 无效。您在两个不应该存在的地方有逗号。在上面我给你的字符串中,这些是固定的。您应该使用以下方式检查 JSON:JSON Formatter

【讨论】:

  • 我之前实际上尝试过实现 GSON,但我遇到了一些错误。这是我关于 GSON 的帖子。 stackoverflow.com/questions/39161354/… 我会再试一次。也许这次我很幸运
  • 在此链接上,我看到您也在使用 JSONArray,然后您正在迭代槽数组,然后使用 Gson。这是不好的。您应该立即使用 Gson 将 json 转换为对象,就像我在上一个答案中给您的示例一样。您还应该知道您的 JSON 格式不正确。在 jsonformatter.curiousconcept.com 上检查您的 json。
  • 我已经更新了答案,通知了无效的 json 和从 JSON 中获取标题的示例。
  • 谢谢,我已经更改了 JSON
  • 我还举例回答了您的其他 GSON 问题。使用 GSON 获取对象列表时需要使用 TypeToken。
【解决方案2】:

使用下面的代码:

exercis.setTarget_id(finalObject.getJSONArray("field_exercis_arc").getString(j));

【讨论】:

    【解决方案3】:
    JsonArray fieldArray=yourJsonObject.getJsonArray("field_exercis_arc");
    for(int i=0;i<fieldArray.length;i++){
     fieldArray.getString(i);
    }
    

    【讨论】:

      【解决方案4】:

      要解析 JSON,您必须这样做。

                  String finalJson = buffer.toString();
                  JSONArray parentArray = new JSONArray(finalJson);
      
                  for(int i=0; i<parentArray.length(); i++){
                      JSONObject finalObject = parentArray.getJSONObject(i);
      
                      String title = finalObject.getString("title");
                      String body = finalObject.getString("body");
      
                      JSONArray arr = finalObject.getJSONArray("field_exercis_arc");
                      for(int x=0; x < arr.length(); x++){
                          String val = arr.getString(x);
                      }
                  }
      

      【讨论】:

      • 谢谢,我已经更改了 JSON
      • 我认为这应该是一条评论
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-20
      • 2021-03-28
      • 2016-01-30
      相关资源
      最近更新 更多