2016-08-29 3 views
0

이 질문은 전에 물어 본 것이 틀림 없습니다.하지만 저는 자바 프로그래밍에서 괴상하기 때문에 도움이 필요합니다. (저는 매우 초보자입니다.) Netbeans를 IDE로 사용합니다. JSON 파일에 문제가 있습니다. 구문 분석하고 싶지만 java에서 수행하는 방법을 모릅니다.자바에서 json .txt 파일을 읽는 방법

여기 내 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":"" 
      } 
       ] 
     } 
    ] 

그리고 내 코드입니다 : 나는 '사용자 이름 "과 바르의 배열로"사용자 ID "저장할

/* 
* 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(); 
    } 

    } 
} 

가. 어떻게해야합니까?

+0

Java 용 JSON의 직렬화를 포함 SO에 꽤 많은 내용이 있습니다. 여기에 몇 가지 있습니다 : http://stackoverflow.com/q/14890129/940217 http://stackoverflow.com/q/9829403/940217 http://stackoverflow.com/q/11106379/940217 http://stackoverflow.com/q/6349421/940217 –

답변

1
  1. 일부 Java 라이브러리 (예 : 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);

체크 아웃이의-부서 튜토리얼 : http://www.java2blog.com/2013/11/gson-example-read-and-write-json.html

관련 문제