Example usage for org.json.simple.parser JSONParser parse

List of usage examples for org.json.simple.parser JSONParser parse

Introduction

In this page you can find the example usage for org.json.simple.parser JSONParser parse.

Prototype

public Object parse(Reader in) throws IOException, ParseException 

Source Link

Usage

From source file:com.AandC.GemsCraft.Configuration.ConfigKey.java

public static int getPort() {
    try {//from   w  ww. j  a v a 2  s. c  o m
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(new FileReader("/sdcard/GemsCraft/config.json"));
        JSONObject jsonObject = (JSONObject) obj;
        port = jsonObject.get("Port");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return port;
}

From source file:com.AandC.GemsCraft.Configuration.ConfigKey.java

public static String getServerName() {
    try {//from   w  w w .  j ava2s .co m
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(new FileReader("/sdcard/GemsCraft/config.json"));
        JSONObject jsonObject = (JSONObject) obj;
        ServerName = String.valueOf(jsonObject.get("ServerName"));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return ServerName;
}

From source file:com.AandC.GemsCraft.Configuration.ConfigKey.java

public static String getMOTD() {
    try {//from www  .j  a va  2s. co  m
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(new FileReader("/sdcard/GemsCraft/config.json"));
        JSONObject jsonObject = (JSONObject) obj;
        MOTD = String.valueOf(jsonObject.get("MOTD"));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return MOTD;
}

From source file:JavaCloud.Utils.java

public static Object request(String address, String function, final JSONObject params) throws CoreException {
    try {/*  w ww. java 2 s. c om*/
        GenericUrl url = new GenericUrl(address + "/" + function);
        NetHttpTransport transport = new NetHttpTransport();
        HttpRequest request = transport.createRequestFactory().buildPostRequest(url, new HttpContent() {
            @Override
            public long getLength() throws IOException {
                return params.toString().length();
            }

            @Override
            public String getType() {
                return "text/json";
            }

            @Override
            public boolean retrySupported() {
                return false;
            }

            @Override
            public void writeTo(OutputStream outputStream) throws IOException {
                outputStream.write(params.toString().getBytes());
            }
        });
        HttpResponse response = request.execute();
        JSONParser parser = new JSONParser();
        JSONObject object = (JSONObject) parser.parse(response.parseAsString());

        if (!object.get("status").equals("ok")) {
            throw new CoreException(object.get("status").toString());
        }

        if (object.containsKey("data") && object.get("data") != null)
            return parser.parse(object.get("data").toString());
        else
            return null;
    } catch (ParseException e) {
        System.out.println("Error parsing response: " + e.toString());
        throw new CoreException("json_malformed");
    } catch (IOException e) {
        System.out.println("Error connecting to server: " + e.toString());
        throw new CoreException("connection_failed");
    }
}

From source file:iracing.webapi.SessionResultDriverLapsParser.java

static SessionResultDriverLaps parse(String json) {
    JSONParser parser = new JSONParser();
    SessionResultDriverLaps output = null;
    try {// ww w . ja  va 2 s  .c  om
        JSONObject root = (JSONObject) parser.parse(json);
        output = new SessionResultDriverLaps();
        JSONObject details = (JSONObject) root.get("details");
        output.setLapsForSolo(getInt(details, "nlapsforsolo"));
        output.setBestNLapsNumber(getInt(details, "bestnlapsnum"));
        output.setSeasonShortName(getString(details, "seasonShortName", true));
        output.setMaximumLicenseLevel(getInt(details, "maxLicenseLevel"));
        output.setDriverName(getString(details, "displayname", true));
        output.setSeasonName(getString(details, "seasonName", true));
        output.setSeriesName(getString(details, "seriesName", true));
        output.setSeriesShortName(getString(details, "seriesShortName", true));
        output.setSessionId(getLong(details, "sessionId"));
        output.setSubSessionId(getLong(details, "subSessionId"));
        output.setCarNumber(getString(details, "carNum"));
        output.setCarId(getInt(details, "carid"));
        output.setEventTypeId(getInt(details, "eventtype"));
        output.setEventTypeName(getString(details, "eventTypeName", true));
        output.setBestQualifyingLapAt(getInt(details, "bestQualLapAt"));
        output.setBestQualifyingLapNumber(getInt(details, "bestQualLapNum"));
        output.setBestLapNumber(getInt(details, "bestLapNum"));
        output.setBestNLapsTime(getInt(details, "bestNLapsTime"));
        output.setLapsForQualifying(getInt(details, "nlapsforqual"));
        output.setBestQualifyingLapTime(getLong(details, "bestQualLapTime"));
        output.setTrackId(getInt(details, "trackID"));
        output.setTrackName(getString(details, "trackName", true));
        output.setTrackConfigName(getString(details, "trackConfig", true));
        output.setEventDate(new Date(getLong(details, "eventDateUTCMilliSecs")));
        List<SessionResultDriverLap> lapList = new ArrayList<SessionResultDriverLap>();
        JSONArray a = (JSONArray) root.get("laps");
        long lastLapTime = 0;
        for (int i = 0; i < a.size(); i++) {
            JSONObject r = (JSONObject) a.get(i);
            SessionResultDriverLap d = new SessionResultDriverLap();
            d.setLapNumber(getInt(r, "lapnum"));
            long lapTime = getLong(r, "time");
            d.setLapTime(lapTime - lastLapTime);
            lastLapTime = lapTime;
            d.setFlags(getInt(r, "flags"));
            lapList.add(d);
        }
        output.setLaps(lapList);
    } catch (ParseException ex) {
        Logger.getLogger(SessionResultDriverLapsParser.class.getName()).log(Level.SEVERE, null, ex);
    }
    return output;
}

From source file:com.serena.rlc.provider.jenkins.domain.Job.java

public static List<Job> parse(String options) {
    List<Job> list = new ArrayList<>();
    JSONParser parser = new JSONParser();
    try {//from ww w. j  ava  2  s  . c om
        Object parsedObject = parser.parse(options);
        JSONArray array = (JSONArray) ((JSONObject) parsedObject).get("jobs");
        for (Object object : array) {
            Job obj = new Job();
            JSONObject jsonObject = (JSONObject) object;

            // JSONObject nameObject = (JSONObject)jsonObject.get("name");
            // JSONObject displayNameObject = (JSONObject)jsonObject.get("displayName");

            obj.setName((String) jsonObject.get("name"));
            obj.setDisplayName((String) jsonObject.get("displayName"));

            list.add(obj);
        }
    } catch (ParseException e) {
        logger.error("Error while parsing input JSON - " + options, e);
    }

    return list;
}

From source file:org.wso2.dss.integration.test.odata.ODataTestUtils.java

public static String getETag(String content) throws ParseException, JSONException {
    JSONParser parser = new JSONParser();
    Object obj = parser.parse(content);
    if (((JSONObject) obj).get("@odata.etag") != null) {
        return ((JSONObject) obj).get("@odata.etag").toString();
    } else {/*from www . ja  va 2 s . c o  m*/
        return ((JSONObject) ((JSONArray) ((JSONObject) obj).get("value")).get(0)).get("@odata.etag")
                .toString();
    }
}

From source file:com.serena.rlc.provider.servicenow.domain.ChangeTask.java

public static ChangeTask parseSingle(String options) {
    JSONParser parser = new JSONParser();
    try {//  ww w.j av  a 2  s.co  m
        Object parsedObject = parser.parse(options);
        ChangeTask changeTask = parseSingle((JSONObject) getJSONValue((JSONObject) parsedObject, "result"));
        return changeTask;
    } catch (ParseException e) {
        logger.error("Error while parsing input JSON - " + options, e);
    }
    return null;
}

From source file:com.serena.rlc.provider.servicenow.domain.Incident.java

public static Incident parseSingle(String options) {
    JSONParser parser = new JSONParser();
    try {/*ww  w. java  2  s.  co m*/
        Object parsedObject = parser.parse(options);
        Incident incident = parseSingle((JSONObject) getJSONValue((JSONObject) parsedObject, "result"));
        return incident;
    } catch (ParseException e) {
        logger.error("Error while parsing input JSON - " + options, e);
    }
    return null;
}

From source file:com.olhcim.engine.Mesh.java

public static Mesh loadMeshFile(String fileName) {
    //load and parse file
    JSONParser parser = new JSONParser();
    Object obj = null;/*  ww w . j a va  2 s.c  o m*/
    try {
        obj = parser.parse(new FileReader(AppMain.class.getResource(fileName).getFile().replace("%20", " ")));
    } catch (IOException ex) {
    } catch (ParseException ex) {
    }
    JSONObject jObj = (JSONObject) obj;

    //load and convert to double array
    Object[] verticesObj = ((JSONArray) jObj.get("positions")).toArray();
    double[] vertices = new double[verticesObj.length];
    for (int i = 0; i < verticesObj.length; i++) {
        vertices[i] = (double) verticesObj[i];
    }

    //load and convert to long array
    Object[] indicesObj = ((JSONArray) jObj.get("indices")).toArray();
    long[] indices = new long[indicesObj.length];
    for (int i = 0; i < indicesObj.length; i++) {
        indices[i] = (long) indicesObj[i];
    }

    int verticesCount = vertices.length / 3;
    int facesCount = indices.length / 3;

    System.out.println("Vertices: " + verticesCount + " Faces: " + facesCount);

    Mesh mesh = new Mesh("", verticesCount, facesCount);

    // Filling the Vertices array of our mesh first
    for (int index = 0; index < verticesCount; index++) {
        float x = (float) vertices[index * 3];
        float y = (float) vertices[index * 3 + 1];
        float z = (float) vertices[index * 3 + 2];
        mesh.vertices[index] = new Vector4f(x, y, z, 1);
    }

    // Then filling the Faces array
    for (int index = 0; index < facesCount; index++) {
        float a = (int) indices[index * 3];
        float b = (int) indices[index * 3 + 1];
        float c = (int) indices[index * 3 + 2];

        mesh.faces[index] = new Vector3f(a, b, c);
    }

    return mesh;
}