Example usage for org.json JSONObject keys

List of usage examples for org.json JSONObject keys

Introduction

In this page you can find the example usage for org.json JSONObject keys.

Prototype

public Iterator keys() 

Source Link

Document

Get an enumeration of the keys of the JSONObject.

Usage

From source file:edu.mit.media.funf.FunfConfig.java

private static Map<String, Bundle[]> getDataRequestMap(JSONObject dataRequestsObject) {
    Map<String, Bundle[]> dataRequestMap = new HashMap<String, Bundle[]>();
    Iterator<String> probeNames = dataRequestsObject.keys();
    try {/*from w  ww  .  j  a va  2s .  c o m*/
        while (probeNames.hasNext()) {
            String probeName = probeNames.next();
            JSONArray requestsJsonArray = dataRequestsObject.getJSONArray(probeName);
            dataRequestMap.put(probeName, getBundleArray(requestsJsonArray));
        }
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
    return dataRequestMap;
}

From source file:edu.mit.media.funf.FunfConfig.java

@SuppressWarnings("unchecked")
private static Bundle getBundle(JSONObject jsonObject) throws JSONException {
    Bundle requestPart = new Bundle();
    Iterator<String> paramNames = jsonObject.keys();
    while (paramNames.hasNext()) {
        String paramName = paramNames.next();
        try {//from   w w  w. j  a v  a  2 s .  c om
            BundleUtil.putInBundle(requestPart, paramName, jsonObject.get(paramName));
        } catch (UnstorableTypeException e) {
            throw new JSONException(e.getLocalizedMessage());
        }
    }
    return requestPart;
}

From source file:com.cognifide.calais.OpenCalaisService.java

private Map<String, List<String>> getTagsFromResponse(String response) throws JSONException {
    Map<String, List<String>> ret = new HashMap<String, List<String>>();
    JSONObject json = new JSONObject(response);
    Iterator<String> i = json.keys();
    while (i.hasNext()) {
        String key = (String) i.next();
        if (key != null) {
            JSONObject item = (JSONObject) json.get(key);
            if (item.has("_typeGroup")) {
                String typeGroup = item.getString("_typeGroup");
                if (typeGroup != null && typeGroup.equals("entities")) {
                    String type = item.getString("_type");
                    String name = item.getString("name");
                    //Double relevance = item.getDouble("relevance");
                    //int count = item.getJSONArray("instances").length();

                    if (!ret.containsKey(type)) {
                        ret.put(type, new ArrayList<String>());
                    }/*from   w  w  w . j a  v a2  s  .co  m*/
                    ret.get(type).add(name);
                }
            }
        }
    }
    return ret;
}

From source file:com.atinternet.tracker.Configuration.java

/**
 * Configuration defined by UI/*from   w w  w.  j  a va 2s .  c o  m*/
 */
Configuration(Context context) {
    JSONObject jsonObject = getDefaultConfiguration(Tool.isTablet(context));
    if (jsonObject != null) {
        Iterator<String> iterator = jsonObject.keys();
        while (iterator.hasNext()) {
            String key = iterator.next();
            try {
                put(key, jsonObject.get(key));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.atinternet.tracker.Configuration.java

/**
 * Override configuration/*from   w  w w .  ja  v  a  2  s.  co  m*/
 *
 * @param configuration HashMap
 */
Configuration(HashMap<String, Object> configuration) {
    clear();
    JSONObject jsonObject = getDefaultConfiguration(false);
    if (jsonObject != null) {
        Iterator<String> iterator = jsonObject.keys();
        while (iterator.hasNext()) {
            String key = iterator.next();
            try {
                put(key, jsonObject.get(key));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
    for (String key : configuration.keySet()) {
        put(key, configuration.get(key));
    }
}

From source file:com.messagesight.mqtthelper.PayloadViewer.java

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.payload);//from  w ww .j a v  a2  s . c  om
    LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("payload"));
    expListView = (ExpandableListView) findViewById(R.id.expandableListView);

    Intent intent = getIntent();
    String jsonString = "";
    jsonString = intent.getStringExtra("json");

    // System.out.println("JSON Present: "+jsonString);
    headers = MqttHandler.getInstance().topicsReceived;
    listChildren = MqttHandler.getInstance().payload;
    payloadAdapter = new PayloadAdapter(this, headers, listChildren);
    expListView.setAdapter(payloadAdapter); //set adapter

    // populate the listView for JSONObject Payloads
    if (jsonString != null) {

        try {
            JSONObject json = new JSONObject(jsonString);

            tempHeaders = new ArrayList<String>();
            tempListChildren = new HashMap<String, List<String>>();
            // headers.clear();
            // listChildren.clear();

            Iterator<?> keys = json.keys();

            while (keys.hasNext()) {
                String key = (String) keys.next();

                tempHeaders.add(key);

                List<String> val = new ArrayList<String>();
                val.add(json.get(key).toString());
                tempListChildren.put(key, val);
            }

            payloadAdapter = new PayloadAdapter(this, tempHeaders, tempListChildren);
            expListView.setAdapter(payloadAdapter);
            isJsonView = true;

        } catch (JSONException e) {

        }
    }

    // Listview on child click listener
    expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition,
                long id) {
            // TODO Auto-generated method stub
            List<String> tempHeadersOnClick;
            HashMap<String, List<String>> tempListChildrenOnClick;
            if (isJsonView) {
                tempHeadersOnClick = tempHeaders;
                tempListChildrenOnClick = tempListChildren;
            } else {
                tempHeadersOnClick = headers;
                tempListChildrenOnClick = listChildren;

            }

            JSONObject json = null;
            try {
                json = new JSONObject(
                        tempListChildrenOnClick.get(tempHeadersOnClick.get(groupPosition)).get(childPosition));
            } catch (JSONException e) {

            }

            if (json != null) {
                Intent intent = new Intent(getApplicationContext(), PayloadViewer.class);
                intent.putExtra("json", json.toString());
                startActivity(intent);
            }
            return false;
        }
    });

}

From source file:com.jennifer.ui.chart.ChartBuilder.java

@Override
public void drawBefore() {

    JSONObject series = cloneObject("series");
    JSONObject grid = cloneObject("grid");
    Object brush = clone("brush");
    Object widget = clone("widget");

    JSONArray data = cloneArray("data");

    // series ?? 
    for (int i = 0, len = data.length(); i < len; i++) {
        JSONObject row = (JSONObject) data.getJSONObject(i);

        Iterator it = row.keys();

        while (it.hasNext()) {
            String key = (String) it.next();

            if (!series.has(key)) {
                series.put(key, new JSONObject());
            }//  w w w . j a  v  a2  s . co m

            JSONObject obj = JSONUtil.clone(series.getJSONObject(key));

            if (obj == null)
                continue;

            Object valueObject = row.get(key);

            if (valueObject instanceof String)
                continue;

            if (valueObject instanceof Double || valueObject instanceof Integer) {
                double value = row.getDouble(key);

                if (!obj.has("min"))
                    obj.put("min", value);
                if (!obj.has("max"))
                    obj.put("max", value);

                if (value < obj.getDouble("min"))
                    obj.put("min", value);
                if (value > obj.getDouble("max"))
                    obj.put("max", value);
            } else if (row.get(key) instanceof Long) {
                long value = row.getLong(key);

                if (!obj.has("min"))
                    obj.put("min", value);
                if (!obj.has("max"))
                    obj.put("max", value);

                if (value < obj.getLong("min"))
                    obj.put("min", value);
                if (value > obj.getLong("max"))
                    obj.put("max", value);
            }

            series.put(key, obj);
        }
    }
    // series_list
    barray("brush", createBrushData(brush, series.names()));
    barray("widget", createBrushData(widget, series.names()));
    bobject("grid", grid);
    bobject("hash", null);
    barray("data", data);
    bobject("series", series);

}

From source file:com.pimp.companionforband.fragments.cloud.ActivitiesFragment.java

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    activitiesLV = (ListView) view.findViewById(R.id.activities_listview);
    statusTV = (TextView) view.findViewById(R.id.status_textview);
    stringArrayList = new ArrayList<>();
    stringArrayAdapter = new ArrayAdapter<>(getContext(), R.layout.activities_list_item,
            R.id.list_item_textView, stringArrayList);
    activitiesLV.setAdapter(stringArrayAdapter);

    RequestQueue queue = Volley.newRequestQueue(getContext());

    JsonObjectRequest activitiesRequest = new JsonObjectRequest(Request.Method.GET,
            CloudConstants.BASE_URL + CloudConstants.Activities_URL, null, new Response.Listener<JSONObject>() {
                @Override/*from   w w  w . j  av  a2 s .com*/
                public void onResponse(JSONObject response) {
                    statusTV.setText("CSV file can be found in CompanionForBand/Activities\n");

                    Iterator<String> stringIterator = response.keys();
                    while (stringIterator.hasNext()) {
                        try {
                            String key = stringIterator.next();
                            JSONArray jsonArray = response.getJSONArray(key);

                            String path = Environment.getExternalStorageDirectory().getAbsolutePath()
                                    + File.separator + "CompanionForBand" + File.separator + "Activities";
                            File file = new File(path);
                            file.mkdirs();

                            JsonFlattener parser = new JsonFlattener();
                            CSVWriter writer = new CSVWriter();
                            try {
                                List<LinkedHashMap<String, String>> flatJson = parser
                                        .parseJson(jsonArray.toString());
                                writer.writeAsCSV(flatJson, path + File.separator + key + ".csv");
                            } catch (Exception e) {
                                Log.e("ActivitiesParseJson", e.toString());
                            }

                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject activity = jsonArray.getJSONObject(i);
                                Iterator<String> iterator = activity.keys();
                                String str = "";
                                while (iterator.hasNext()) {
                                    key = iterator.next();
                                    str = str + UIUtils.splitCamelCase(key) + " : "
                                            + activity.get(key).toString() + "\n";
                                }
                                stringArrayAdapter.add(str);
                            }
                        } catch (Exception e) {
                            Log.e("Activities", e.toString());
                        }
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getActivity(), error.toString(), Toast.LENGTH_LONG).show();
                }
            }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<>();
            headers.put("Authorization",
                    "Bearer " + MainActivity.sharedPreferences.getString("access_token", "hi"));

            return headers;
        }
    };

    queue.add(activitiesRequest);
}