MyTest.ParseJson.java Source code

Java tutorial

Introduction

Here is the source code for MyTest.ParseJson.java

Source

/*
 * 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 MyTest;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
 *
 * @author AiWangtao
 */
public class ParseJson {

    public static void main(String args[]) {
        try {
            FileReader reader = new FileReader(
                    new File("data/NGS___RNA-seq_differential_expression_analysis-v1.ga"));
            System.out.println(reader);
            JSONParser parser = new JSONParser();
            JSONObject jo = (JSONObject) parser.parse(reader);
            JSONObject jsteps = (JSONObject) jo.get("steps");

            System.out.println(jsteps.size());
            System.out.println("---------------------------------------------------");
            for (int i = 0; i < jsteps.size(); i++) {
                JSONObject jstep_detail = (JSONObject) jsteps.get(String.valueOf(i));
                getDetailInfo(jstep_detail);

            }

        } catch (FileNotFoundException ex) {
            Logger.getLogger(ParseJson.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ParseJson.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            Logger.getLogger(ParseJson.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private static void getDetailInfo(JSONObject jstep_detail) {
        //        System.out.println(jstep_detail);
        long id = (long) jstep_detail.get("id");
        System.out.println("id:" + id);
        String name = (String) jstep_detail.get("name");
        System.out.println("name:" + name);
        JSONArray inputs = (JSONArray) jstep_detail.get("inputs");
        if (inputs.isEmpty()) {
            System.out.println("inputs:null");

        } else {
            System.out.println("inputs:");
            for (int i = 0; i < inputs.size(); i++) {
                System.out.println(inputs.get(i) + "\n");
            }
        }

        JSONArray outpus = (JSONArray) jstep_detail.get("outputs");
        if (outpus.isEmpty()) {
            System.out.println("outpus:null");
        } else {
            System.out.println("outpus:");
            for (int i = 0; i < outpus.size(); i++) {
                System.out.print(outpus.get(i) + "\n");
            }
        }

        JSONObject links = (JSONObject) jstep_detail.get("input_connections");
        if (links.isEmpty()) {
            System.out.println("input_connections:null");
        } else {
            Iterator it = links.keySet().iterator();
            System.out.println("input_connections: ");
            while (it.hasNext()) {
                Object input_link = it.next();
                System.out.println(links.get(input_link));
            }
        }
        System.out.println("-------------------------------------------------------");
    }

}