简体   繁体   English

如何从 JSON 文件动态获取数据

[英]how to get data from JSON file dynamically

So i made this mini app in purpose of learning JSON所以我制作了这个迷你应用程序来学习 JSON

so basically this is the JSON file所以基本上这是 JSON 文件

{
  "Student1" : {
    "name" : "Amine" ,
    "mark": 19,
    "Groupe": "G25"
  },
  "Student2" : {
    "name" : "Zaki" ,
    "mark": 19,
    "groupe": "G25",
  }

}

i tried instead of Putting "Student1" i thought i could do something like "Student"+"1"我试过而不是把“学生1”我想我可以做一些像“学生”+“1”

  private void parseJson(String toString) {

        TextView showView = new TextView(this);

        theLayout.addView(showView);

        StringBuilder builder = new StringBuilder();

        try {
            JSONObject root = new JSONObject(toString);

 /////// Here i got the INPUT string from an EditText 
///// i think i should use some thing like .equals() 

             String theWantedStudent = "Student"+INPUT ;

            JSONObject student = root.getJSONObject(theWantedStudent);



            builder.append("Name : ")
                    .append(student.getString("name")).append("\n");
            builder.append("Groupe : ")
                    .append(student.getString("groupe")).append("\n");
            builder.append("Mark : ")
                    .append(student.getInt("mark")).append("\n");

        } catch (JSONException e) {
            e.printStackTrace();
        }


        showView.setText(builder.toString());

    }

if there is a simpler methode let me know thanks如果有更简单的方法,请告诉我谢谢

I recommend to use Gson to parse the JSON into an object.我建议使用 Gson 将 JSON 解析为 object。 After that, it will be easier to manipulate the data.之后,操作数据将更容易。

First, create a Java class with yout JSON structure:首先,用你的 JSON 结构创建一个 Java class :

 public class Student {
       private String name;
       private int mark;
       private String group;

// add the getters and setters
} 

Then, use Gson to parse to the object:然后,使用 Gson 解析为 object:

    Student student = new Gson().fromJson(json, Student.class);

If you want to build a JSON from an object use the Gson's method toJson:如果要从 object 构建 JSON,请使用 Gson 的方法 toJson:

Student student = new Student();
student.setName("Peter");
student.setMark(10);
student.setGroup("ABC");

String json = new Gson().toJson(student);

If you wanna more information about Gson check this tutorial, it's really good: https://mkyong.com/java/how-to-parse-json-with-gson/如果您想了解有关 Gson 的更多信息,请查看本教程,它非常好: https://mkyong.com/java/how-to-parse-json-with-gson/

I think what you want is a list of student ojects:我认为您想要的是学生项目列表:

[
  {
    "name" : "Amine" ,
    "mark": 19,
    "groupe": "G25"
  },
  {
    "name" : "Zaki" ,
    "mark": 19,
    "groupe": "G25"
  }
]

Then use a class such as:然后使用 class 例如:

public class Student {
    @JsonProperty("name")
    private String name;
    @JsonProperty("mark")
    private Integer mark;
    @JsonProperty("groupe")
    private String groupe;

    //getters and setters    
}

Then use jackson to iterate thru the list然后使用 jackson 遍历列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM