如何在java中读取json .txt文件

时间:2016-08-29 23:46:05

标签: java json

我确信之前已经问过这个问题,但我确实需要帮助,因为我是java编程的书呆子(我真的非常初学者)。 我使用Netbeans作为IDE。有关JSON文件的问题,我想解析它但不知道如何在java中执行它。

这是我的JSON文件:

    [
       {
          "rows":[
             {
                "positionId":1,
                "userName":"Abay Nurpeissov/ Абай Нурпеисов",
                "vacation":"",
                "position":"Student",
                "officePhone":"",
                "mobilePhone":"",
                "email":"",
                "userid":"201641686",
                "groupEmail":"",
                "departmentPathEN":""
             },
             {
                "positionId":1,
                "userName":"Abilmansur Yeshmuratov/ Абильмансур Ешмуратов",
                "vacation":"",
                "position":"Student",
                "officePhone":"",
                "mobilePhone":"",
                "email":"",
                "userid":"201551882",
                "groupEmail":"",
                "departmentPathEN":""
             }
                ]
      }
   ]

这是我的代码:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package kyzdarkz;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.poi.hssf.usermodel.HSSFSheet;
/**
 *
 * @author anuar
 */
public class Kyzdarkz {

    //private ArrayList<Student> students = new ArrayList();

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {

        try(BufferedReader br = new BufferedReader(new FileReader("json.txt"))) {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

        while (line != null) {
            sb.append(line);
            sb.append(System.lineSeparator());
            line = br.readLine();
        }
        String everything = sb.toString();
    }  

    }
}

我想将“userName”和“userid”保存为vars数组。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

  1. 使用一些Java lib :(例如:Gson,jackson)并将其导入您的项目中。
  2. 创建一个与输入json对应的类。例如:

    公共阶层Emp {

    private String positionId;
    private String username;
    private String postion;
    
    //and so on ...
    
    public String getPositionId() {
        return positionId;
    }
    public void setPositionId(String positionId) {
        this.positionId = positionId;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPostion() {
        return postion;
    }
    public void setPostion(String postion) {
        this.postion = postion;
    }
    
    //and so on ....
    

    }

  3. 在你的方法中使用它。

    Gson gson = new Gson();
    Emp [] person = gson.fromJson(input,Emp [] .class);

  4. 查看此部门内部教程:http://www.java2blog.com/2013/11/gson-example-read-and-write-json.html