1

I am sure that this question has been asked before, but I do need help because I am nerd in the java programming (I am really very beginner). I use Netbeans as an IDE. Problem with the JSON file, I want to parse it but don't know how to do it in java.

Here's my JSON file looks like:

    [
       {
          "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":""
             }
                ]
      }
   ]

And here's my code:

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

    }
}

I want to save "userName" and "userid" as an array of vars. How can I do that?

Anuar Maratkhan
  • 325
  • 5
  • 21
  • There are quite a few topics on SO which cover JSON deserialization for Java. Here are some: http://stackoverflow.com/q/14890129/940217 http://stackoverflow.com/q/9829403/940217 http://stackoverflow.com/q/11106379/940217 http://stackoverflow.com/q/6349421/940217 – Kyle Falconer Aug 29 '16 at 23:52

1 Answers1

1
  1. Use some Java lib: (eg: Gson, jackson) and import it in your project.
  2. Create a class corresponding to input json. For eg:

    public class 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. Use this in you method.

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

Check out this in-dept tutorial: http://www.java2blog.com/2013/11/gson-example-read-and-write-json.html

voldy
  • 359
  • 1
  • 8
  • 21