Java Code Examples for org.json.JSONArray#iterator()

The following examples show how to use org.json.JSONArray#iterator() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonUtil.java    From LicenseScout with Apache License 2.0 6 votes vote down vote up
/**
 * @param file the package.json file
 * @return a license name or an empty string if the package.json does not contain a 'license' tag
 * @throws IOException if an error occurred while reading the JSON file
 */
public static String getNPMArchiveLicenseName(final File file) throws IOException {
    final JSONObject obj = readJsconFile(file);
    final JSONArray licensesArray = obj.optJSONArray("licenses");
    if (licensesArray != null) {
        final Iterator<Object> iter = licensesArray.iterator();
        while (iter.hasNext()) {
            Object o = iter.next();
            if (o instanceof JSONObject) {
                final JSONObject jobj = (JSONObject) o;
                final String typeValue = jobj.optString("type");
                if (typeValue != null) {
                    return typeValue;
                }
            }
        }
    }
    final JSONObject licenseObject = obj.optJSONObject("license");
    if (licenseObject != null) {
        return licenseObject.optString("type");
    }
    return obj.optString("license");
}
 
Example 2
Source File: Bilibili.java    From VideoHelper with MIT License 6 votes vote down vote up
/**
 * timeLength, fileSize, urls
 *
 * @param avReqApi
 * @throws IOException
 */
private void parseAvApiResponse(String avReqApi) throws IOException {
    String result = HttpUtil.getResponseContent(avReqApi);

    // println(result);

    JSONObject jsonObject = new JSONObject(result);
    timeLength = jsonObject.getInt("timelength");

    JSONArray ja = jsonObject.getJSONArray("durl");

    Iterator<Object> iterator = ja.iterator();
    while (iterator.hasNext()) {
        JSONObject jb = (JSONObject) iterator.next();

        String videoSrc = jb.getString("url");
        urls.add(videoSrc);

        fileSize += jb.getInt("size");
    }
}
 
Example 3
Source File: AutoScalerEnvUtil.java    From open-Autoscaler with Apache License 2.0 6 votes vote down vote up
public static AppEnv getApplicationEnv(){
	String applicationEnv = System.getenv(Constants.VCAP_APPLICATION_ENV);
	if (applicationEnv == null)
		return null;
	AppEnv appEnv = new AppEnv();
	try {
		JSONObject jsonObj = new JSONObject(applicationEnv);
		JSONArray array = (JSONArray)jsonObj.get("application_uris");
		String[] uris = new String[array.length()];
		Iterator iter = array.iterator();
		int i = 0;
		while (iter.hasNext()){
			uris[i] = iter.next().toString(); 
		}
		appEnv.setApplication_uris(uris);
	} catch (Exception e) {
		logger.error( "Error to parse application environment varables");
	}
	return appEnv;
}
 
Example 4
Source File: JSONArrayTest.java    From JSON-Java-unit-test with Apache License 2.0 5 votes vote down vote up
/**
 * Exercise the JSONArray iterator.
 */
@SuppressWarnings("boxing")
@Test
public void iterator() {
    JSONArray jsonArray = new JSONArray(this.arrayStr);
    Iterator<Object> it = jsonArray.iterator();
    assertTrue("Array true",
            Boolean.TRUE.equals(it.next()));
    assertTrue("Array false",
            Boolean.FALSE.equals(it.next()));
    assertTrue("Array string true",
            "true".equals(it.next()));
    assertTrue("Array string false",
            "false".equals(it.next()));
    assertTrue("Array string",
            "hello".equals(it.next()));

    assertTrue("Array double",
            new Double(23.45e-4).equals(it.next()));
    assertTrue("Array string double",
            new Double(23.45).equals(Double.parseDouble((String)it.next())));

    assertTrue("Array value int",
            new Integer(42).equals(it.next()));
    assertTrue("Array value string int",
            new Integer(43).equals(Integer.parseInt((String)it.next())));

    JSONArray nestedJsonArray = (JSONArray)it.next();
    assertTrue("Array value JSONArray", nestedJsonArray != null);

    JSONObject nestedJsonObject = (JSONObject)it.next();
    assertTrue("Array value JSONObject", nestedJsonObject != null);

    assertTrue("Array value long",
            new Long(0).equals(((Number) it.next()).longValue()));
    assertTrue("Array value string long",
            new Long(-1).equals(Long.parseLong((String) it.next())));
    assertTrue("should be at end of array", !it.hasNext());
}
 
Example 5
Source File: SyncOrgPanel.java    From xyTalk-pc with GNU Affero General Public License v3.0 4 votes vote down vote up
private void syncAllUser() {
	// TODO Auto-generated method stub
   	Launcher.contactsUserService.deleteAll();

   	long startTime = System.currentTimeMillis();

	JSONArray jsons;
	try {
		jsons = JsonUtil.readJsonsFromUrl(Launcher.ORGUSERURL);
		int count = jsons.length();
		double step = count/100;
		int i = 0;

		Iterator<Object> it = jsons.iterator();
           List<ContactsUser> list=new ArrayList<ContactsUser>();
           while (it.hasNext()) {
               JSONObject ob = (JSONObject) it.next();
               ContactsUser model = null;
               if(ob.getString("n")!=null){
                   model=new ContactsUser();
                   model.setName(ob.getString("n"));
               }
               if(ob.getString("u")!=null){
                   model.setUsername(ob.getString("u"));
                   model.setUserId(ob.getString("u"));
               }
               if(ob.getString("p")!=null){
                   model.setPhone(ob.getString("p"));
               }
               if(ob.getString("s")!=null){
                   model.setSp(ob.getString("s"));
               }
               if(ob.getString("d")!=null){
                   model.setDept(ob.getString("d"));
               }
               if(ob.getString("e")!=null){
                   model.setMail(ob.getString("e"));
               }
               if(ob.getString("l")!=null){
                   model.setLocation(ob.getString("l"));
               }
               i++;
               if(model!=null){
                   list.add(model);
                   Launcher.contactsUserService.insert(model);
                   int prec = i/(int)step;
                   progressBar.setValue(prec);
                   infoLabel.setText(prec + "%");
               }
           }			 

	} catch (JSONException e) {
		// TODO Auto-generated catch block
		
		e.printStackTrace();
	}
	long endTime = System.currentTimeMillis() - startTime  ;
    DebugUtil.debug("同步用户查询耗时(毫秒):"+ endTime );

       RightPanel.getContext().getTitlePanel().showAppTitle("联系人已同步为最新");
       RightPanel.getContext().showPanel(RightPanel.TIP);
}
 
Example 6
Source File: SimulationUserData.java    From xyTalk-pc with GNU Affero General Public License v3.0 4 votes vote down vote up
private static void syncAllUser() {
	// TODO Auto-generated method stub
   	Launcher.contactsUserService.deleteAll();

   	long start200 = System.currentTimeMillis();

	JSONArray jsons;
	try {
		jsons = JsonUtil.readJsonsFromUrl("http://111.230.157.216/200jsonUTF8.txt");
		int count = jsons.length();
		double step = count/100;
		int i = 0;

		Iterator<Object> it = jsons.iterator();
           List<ContactsUser> list=new ArrayList<ContactsUser>();
           while (it.hasNext()) {
               JSONObject ob = (JSONObject) it.next();
               ContactsUser model = null;
               if(ob.getString("n")!=null){
                   model=new ContactsUser();
                   model.setName(ob.getString("n"));
               }
               if(ob.getString("u")!=null){
                   model.setUsername(ob.getString("u"));
                   model.setUserId(ob.getString("u"));
               }
               if(ob.getString("p")!=null){
                   model.setPhone(ob.getString("p"));
               }
               if(ob.getString("s")!=null){
                   model.setSp(ob.getString("s"));
               }
               if(ob.getString("d")!=null){
                   model.setDept(ob.getString("d"));
               }
               if(ob.getString("e")!=null){
                   model.setMail(ob.getString("e"));
               }
               if(ob.getString("l")!=null){
                   model.setLocation(ob.getString("l"));
               }
               i++;
               if(model!=null){
                   list.add(model);
                   Launcher.contactsUserService.insert(model);

               }

           }			 

	} catch (JSONException e) {
		// TODO Auto-generated catch block
		
		e.printStackTrace();
	}
	long time200 = System.currentTimeMillis() - start200  ;
    System.out.println("200个对象查询耗时(毫秒):"+time200 );


}
 
Example 7
Source File: JSONObjectIterator.java    From tutorials with MIT License 4 votes vote down vote up
public void handleJSONArray(String key, JSONArray jsonArray) {
    Iterator<Object> jsonArrayIterator = jsonArray.iterator();
    jsonArrayIterator.forEachRemaining(element -> {
        handleValue(key, element);
    });
}