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

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

Introduction

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

Prototype

JSONParser

Source Link

Usage

From source file:Bean.ObjectStorageConnector.java

public void getConnection() {

    try {//w  w w .jav  a 2 s . co  m
        String envApp = System.getenv("VCAP_APPLICATION");
        String envServices = System.getenv("VCAP_SERVICES");

        JSONParser parser = new JSONParser();
        Object obj = parser.parse(envServices);
        JSONObject jsonObject = (JSONObject) obj;
        JSONArray vcapArray = (JSONArray) jsonObject.get("Object-Storage");
        JSONObject vcap = (JSONObject) vcapArray.get(0);
        JSONObject credentials = (JSONObject) vcap.get("credentials");
        auth_url = credentials.get("auth_url").toString() + "/v3";
        project = credentials.get("project").toString();
        projectId = credentials.get("projectId").toString();
        region = credentials.get("region").toString();
        userId = credentials.get("userId").toString();
        username = credentials.get("username").toString();
        password = credentials.get("password").toString();
        domainId = credentials.get("domainId").toString();
        domainName = credentials.get("domainName").toString();

        Identifier domainIdent = Identifier.byName(domainName);
        Identifier projectIdent = Identifier.byName(project);

        os = OSFactory.builderV3().endpoint(auth_url).credentials(userId, password)
                .scopeToProject(projectIdent, domainIdent).authenticate();

        account = os.objectStorage().account().get();

    } catch (ParseException ex) {
    }
}

From source file:net.sourceforge.fenixedu.dataTransferObject.externalServices.TeacherPublicationsInformation.java

public static Map<Teacher, List<String>> getTeacherPublicationsInformations(Set<Teacher> teachers) {
    Map<Teacher, List<String>> teacherPublicationsInformationMap = new HashMap<Teacher, List<String>>();

    Client client = ClientBuilder.newClient();
    WebTarget resource = client.target(BASE_URL);
    List<String> teacherIds = new ArrayList<String>();

    for (Teacher teacher : teachers) {
        teacherIds.add(teacher.getTeacherId());
    }/*from w w w.  j a  va 2s .  co  m*/

    resource = resource.path(CURRICULUM_PATH).queryParam("istids", StringUtils.join(teacherIds, ","));
    try {
        String allPublications = resource.request().get(String.class);
        JSONParser parser = new JSONParser();
        for (Object teacherPublications : (JSONArray) parser.parse(allPublications)) {
            JSONObject teacherPublicationsInfo = (JSONObject) teacherPublications;
            final String username = (String) teacherPublicationsInfo.get("istID");
            final Teacher teacher = Teacher.readByIstId(username);
            JSONArray preferredPublications = (JSONArray) teacherPublicationsInfo.get("preferred");

            List<String> teacherPublicationsList = new ArrayList<String>();
            for (Object publication : preferredPublications) {
                teacherPublicationsList.add(publication.toString());
            }

            teacherPublicationsInformationMap.put(teacher, teacherPublicationsList);
        }
    } catch (ParseException e) {
        logger.error(e.getMessage(), e);
    } finally {
        client.close();
    }

    return teacherPublicationsInformationMap;
}

From source file:buspathcontroller.JSONFileParser.java

public void generateAllStops() {
    JSONParser parser = new JSONParser();
    try {// w  w  w.ja v  a  2  s  .c om
        PrintWriter writer = new PrintWriter("/Users/Zhaowei/Desktop/BusPath/allStops.txt");
        Object obj = parser.parse(new FileReader("/Users/Zhaowei/Desktop/BusPath/RawJSON/StopList.json"));
        JSONObject jsonObject = (JSONObject) obj;
        JSONArray routes = (JSONArray) jsonObject.get("stops");
        Iterator iterator = routes.iterator();
        while (iterator.hasNext()) {
            JSONObject stop = (JSONObject) ((JSONObject) iterator.next()).get("stop");
            writer.println(stop.get("stopnumber") + ";" + stop.get("stoptitle") + ";" + stop.get("stoplat")
                    + ";" + stop.get("stoplng"));
        }
        writer.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:io.github.seanboyy.lotReset.json.ReadJSON.java

/**Read JSON file by using configuration file's JSON value,
 * which points to the file//from   w  w w .  j av  a 2s .  c o  m
 * @param configLocation String which specifies the location of the config.properties file
 * @return <code>ArrayList&ltArrayList&ltLot&gt&gt lots</code> if no errors are encountered
 * @since 1.0 - Implemented config.properties in 2.0
 */
public static ArrayList<ArrayList<Lot>> read(String configLocation) {
    JSONParser parser = new JSONParser();
    URL url;
    File f;
    //lots is multidimensional in this format: {{fromLot,toLot},{fromLot,toLot},{fromLot,toLot},etc...}
    ArrayList<ArrayList<Lot>> lots = new ArrayList<ArrayList<Lot>>();
    Config config = ReadConfig.read(configLocation);
    final String[] alphabet = config.getAlpha();
    final String[] types = config.getType();
    final String[] worlds = config.getWorld();
    try {
        url = new URL(config.getJSON());
        InputStream in = url.openStream();
        f = File.createTempFile("temp", "json");
        FileWriter file = new FileWriter(f);
        Scanner input = new Scanner(in);
        while (input.hasNextLine()) {
            file.write(input.nextLine());
        }
        input.close();
        file.close();
        Object obj = parser.parse(new FileReader(f));
        JSONObject jsonObj = (JSONObject) obj;
        JSONObject regions = (JSONObject) jsonObj.get("Regions");
        for (String a : worlds) {
            for (String b : types) {
                for (String c : alphabet) {
                    for (int d = 1; d <= alphabet.length; ++d) {
                        String lotId = a + "-" + b + "-" + c + d;
                        String lotIdA = a + "_" + b + "-" + c + d;
                        String lotIdB = a + "-" + b + "_" + c + d;
                        String lotIdC = a + "_" + b + "_" + c + d;
                        JSONObject lot = (JSONObject) regions.get(lotId);
                        JSONObject lotA = (JSONObject) regions.get(lotIdA);
                        JSONObject lotB = (JSONObject) regions.get(lotIdB);
                        JSONObject lotC = (JSONObject) regions.get(lotIdC);
                        ArrayList<Lot> lotInfo = new ArrayList<Lot>();
                        if (lot != null) {
                            lotInfo.add(new Lot((long) lot.get("source_minX"), (long) lot.get("source_maxX"),
                                    (long) lot.get("source_minY"), (long) lot.get("source_maxY"),
                                    (long) lot.get("source_minZ"), (long) lot.get("source_maxZ"),
                                    (String) lot.get("source_file"), lotId));
                            lotInfo.add(new Lot((long) lot.get("dest_minX"), (long) lot.get("dest_maxX"),
                                    (long) lot.get("dest_minY"), (long) lot.get("dest_maxY"),
                                    (long) lot.get("dest_minZ"), (long) lot.get("dest_maxZ"),
                                    (String) lot.get("dest_file"), lotId));
                            lots.add(lotInfo);
                        }
                        if (lotA != null) {
                            lotInfo.add(new Lot((long) lotA.get("source_minX"), (long) lotA.get("source_maxX"),
                                    (long) lotA.get("source_minY"), (long) lotA.get("source_maxY"),
                                    (long) lotA.get("source_minZ"), (long) lotA.get("source_maxZ"),
                                    (String) lotA.get("source_file"), lotIdA));
                            lotInfo.add(new Lot((long) lotA.get("dest_minX"), (long) lotA.get("dest_maxX"),
                                    (long) lotA.get("dest_minY"), (long) lotA.get("dest_maxY"),
                                    (long) lotA.get("dest_minZ"), (long) lotA.get("dest_maxZ"),
                                    (String) lotA.get("dest_file"), lotIdA));
                            lots.add(lotInfo);
                        }
                        if (lotB != null) {
                            lotInfo.add(new Lot((long) lotB.get("source_minX"), (long) lotB.get("source_maxX"),
                                    (long) lotB.get("source_minY"), (long) lotB.get("source_maxY"),
                                    (long) lotB.get("source_minZ"), (long) lotB.get("source_maxZ"),
                                    (String) lotB.get("source_file"), lotIdB));
                            lotInfo.add(new Lot((long) lotB.get("dest_minX"), (long) lotB.get("dest_maxX"),
                                    (long) lotB.get("dest_minY"), (long) lotB.get("dest_maxY"),
                                    (long) lotB.get("dest_minZ"), (long) lotB.get("dest_maxZ"),
                                    (String) lotB.get("dest_file"), lotIdB));
                            lots.add(lotInfo);
                        }
                        if (lotC != null) {
                            lotInfo.add(new Lot((long) lotC.get("source_minX"), (long) lotC.get("source_maxX"),
                                    (long) lotC.get("source_minY"), (long) lotC.get("source_maxY"),
                                    (long) lotC.get("source_minZ"), (long) lotC.get("source_maxZ"),
                                    (String) lotC.get("source_file"), lotIdC));
                            lotInfo.add(new Lot((long) lotC.get("dest_minX"), (long) lotC.get("dest_maxX"),
                                    (long) lotC.get("dest_minY"), (long) lotC.get("dest_maxY"),
                                    (long) lotC.get("dest_minZ"), (long) lotC.get("dest_maxZ"),
                                    (String) lotC.get("dest_file"), lotIdC));
                            lots.add(lotInfo);
                        }
                    }
                }
            }
        }
    } catch (IOException e) {
        System.out.println("FILE ERROR FROM READING JSON");
        e.printStackTrace();
        return null;
    } catch (ParseException e) {
        System.out.println("PARSER ERROR FROM READING JSON");
        e.printStackTrace();
        return null;
    }
    return lots;
}

From source file:com.mp.gw2api.data.GW2APIMap.java

@Override
public void LoadFromJsonText(String text) {
    JSONParser parser = new JSONParser();
    JSONObject json = null;//from   w  ww  .j a v a2 s.c o  m
    JSONArray ja = null;
    JSONArray jb = null;

    try {
        //Parse json array
        json = (JSONObject) parser.parse(text);

        id = (long) json.get("id");
        name = (String) json.get("name");
        minLevel = (long) json.get("min_level");
        maxLevel = (long) json.get("max_level");
        defFloor = (long) json.get("default_floor");

        ja = (JSONArray) json.get("floors");
        if (ja != null)
            if (ja.size() > 0) {
                floors = new long[ja.size()];
                for (int i = 0; i < ja.size(); i++)
                    floors[i] = (long) ja.get(i);
            }

        region = new GW2APIMapRegion((long) json.get("region_id"), (String) json.get("region_name"));
        continent = new GW2APIMapContinent((long) json.get("continent_id"),
                (String) json.get("continent_name"));

        mapRect = new PointRectangle();
        ja = (JSONArray) json.get("map_rect");
        jb = (JSONArray) ja.get(0);
        mapRect.begin.x = (long) jb.get(0);
        mapRect.begin.y = (long) jb.get(1);
        jb = (JSONArray) ja.get(1);
        mapRect.end.x = (long) jb.get(0);
        mapRect.end.y = (long) jb.get(1);

        contRect = new PointRectangle();
        ja = (JSONArray) json.get("continent_rect");
        jb = (JSONArray) ja.get(0);
        contRect.begin.x = (long) jb.get(0);
        contRect.begin.y = (long) jb.get(1);
        jb = (JSONArray) ja.get(1);
        contRect.end.x = (long) jb.get(0);
        contRect.end.y = (long) jb.get(1);

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

From source file:it.polimi.logging.LogMessageUtils.java

public static String getTopicReply(String message) {
    JSONParser parser = new JSONParser();
    JSONObject jsonMsg;//from   w  w  w.  j a  v  a  2  s. c om
    String topic = "";
    try {
        jsonMsg = (JSONObject) parser.parse(message);
        topic = (String) jsonMsg.get(JsonStrings.TOPIC_REPLY);
        return topic;
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
}

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

public static List<ChangeRequest> parse(String options) {
    List<ChangeRequest> crList = new ArrayList<>();
    JSONParser parser = new JSONParser();
    try {//from ww w  . j a  v  a2  s . c  o m
        Object parsedObject = parser.parse(options);
        JSONArray jsonArray = (JSONArray) getJSONValue((JSONObject) parsedObject, "result");
        for (Object object : jsonArray) {
            ChangeRequest crObj = parseSingle((JSONObject) object);
            crList.add(crObj);
        }
    } catch (ParseException e) {
        logger.error("Error while parsing input JSON - " + options, e);
    }
    return crList;
}

From source file:com.eduonix.kafka.ParseTwitterStreamTask.java

public void process(IncomingMessageEnvelope envelope, MessageCollector collector, TaskCoordinator coordinator)
        throws Exception {

    String msg = ((String) envelope.getMessage());

    try {//from w w  w. j  a  v  a2  s  .c o  m
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(msg);
        JSONObject jsonObj = (JSONObject) obj;
        String text = (String) jsonObj.get("text");
        if (text == null || text.trim().isEmpty()) {
            text = "found text == null ||text.trim().isEmpty ";
        }

        collector.send(new OutgoingMessageEnvelope(new SystemStream("kafka", "tweets-parsed"), text));
    } catch (ParseException pe) {
    }

}

From source file:org.kitodo.data.elasticsearch.index.type.RulesetTypeTest.java

@Test
public void shouldCreateDocument() throws Exception {
    RulesetType rulesetType = new RulesetType();
    Ruleset ruleset = prepareData().get(0);
    JSONParser parser = new JSONParser();

    HttpEntity document = rulesetType.createDocument(ruleset);
    JSONObject actual = (JSONObject) parser.parse(EntityUtils.toString(document));
    JSONObject excepted = (JSONObject) parser.parse(
            "{\"title\":\"SLUBDD\",\"file\":\"ruleset_slubdd.xml\",\"orderMetadataByRuleset\":false\"fileContent\":\"\"}");
    assertEquals("Ruleset JSONObject doesn't match to given JSONObject!", excepted, actual);
}

From source file:com.conwet.silbops.msg.PublishMsgTest.java

@Test
public void testJSONRoundtrip() throws ParseException {
    PublishMsg msg = new PublishMsg();
    msg.setNotification(/*w  w  w .j a  v  a2s  .c  o m*/
            new Notification().attribute("attr1", Type.STRING, "val1").attribute("attr2", Type.STRING, "one"));
    msg.setContext(new Context());

    String jsonStr = msg.toJSONString();
    JSONObject json = (JSONObject) new JSONParser().parse(jsonStr);
    assertThat(PublishMsg.fromJSON(json)).isEqualTo(msg);
}