org.json.JSONObject.get()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(11.9k)|赞(0)|评价(0)|浏览(191)

本文整理了Java中org.json.JSONObject.get()方法的一些代码示例,展示了JSONObject.get()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JSONObject.get()方法的具体详情如下:
包路径:org.json.JSONObject
类名称:JSONObject
方法名:get

JSONObject.get介绍

[英]Returns the value mapped by name.
[中]返回按名称映射的值。

代码示例

canonical example by Tabnine

public void accessingJson(JSONObject json) {
  Object invalid = json.get("invalid"); // throws JSONException - "invalid" entry doesn't exists
  String name = json.getString("name"); // name = "John Brown"
  int number = json.getInt("name"); // throws JSONException - "name" entry isn't int
  int age = json.optInt("age", 42); // using default value instead of throwing exception
  JSONArray pets = json.getJSONArray("pets");
  for (int i = 0; i < pets.length(); i++) {
    String pet = pets.getString(i);
  }
}

代码示例来源:origin: stackoverflow.com

JSONObject json = (JSONObject)new JSONParser().parse("{\"name\":\"MyNode\", \"width\":200, \"height\":100}");
System.out.println("name=" + json.get("name"));
System.out.println("width=" + json.get("width"));

代码示例来源:origin: stackoverflow.com

String jString = "{\"a\": 1, \"b\": \"str\"}";
JSONObject jObj = new JSONObject(jString);
Object aObj = jObj.get("a");
if(aObj instanceof Integer){
  System.out.println(aObj);
}

代码示例来源:origin: facebook/facebook-android-sdk

private static void putImageInBundleWithArrayFormat(
    Bundle parameters,
    int index,
    JSONObject image) throws JSONException{
  Iterator<String> keys = image.keys();
  while (keys.hasNext()) {
    String property = keys.next();
    String key = String.format(Locale.ROOT, "image[%d][%s]", index, property);
    parameters.putString(key, image.get(property).toString());
  }
}

代码示例来源:origin: stackoverflow.com

JSONParser parser = new JSONParser();

Object obj = parser.parse(new FileReader("simple.json"));

JSONObject jsonObject = (JSONObject) obj;

for(Iterator iterator = jsonObject.keySet().iterator(); iterator.hasNext();) {
  String key = (String) iterator.next();
  System.out.println(jsonObject.get(key));
}

代码示例来源:origin: stackoverflow.com

Iterator<String> it=obj.keys();
    while(it.hasNext()){
String keys=it.next();
JSONObject innerJson=new JSONObject(obj.toString());
JSONArray innerArray=innerJson.getJSONArray(keys);
for(int i=0;i<innerArray.length();i++){
JSONObject innInnerObj=innerArray.getJSONObject(i);
Iterator<String> InnerIterator=innInnerObj.keys();
while(InnerIterator.hasNext()){
System.out.println("InnInnerObject value is :"+innInnerObj.get(InnerIterator.next()));

 }
}

代码示例来源:origin: stackoverflow.com

HashMap<String,String> hashOut = new HashMap<String,String>();
 String input = "{\"others\":[    {        \"id\":\"1\",        \"caption\":\"test\"    },    {        \"id\":\"2\",        \"caption\":\"self\"    },    {        \"id\":\"2\",        \"caption\":\"self\"    },    {        \"id\":\"1\",        \"caption\":\"test\"    }],\"quantity\":[    {        \"id\":\"1\",        \"caption\":\"self1\"    },    {        \"id\":\"1\",        \"caption\":\"self1\"    }]}";
 JSONObject jsonObj = new JSONObject(input);
 Iterator it = jsonObj.keys();
 while (it.hasNext()) {
   hashOut.clear();
   JSONArray jsInner = (JSONArray) jsonObj.getJSONArray((String) it.next());
   for (int i=0;i<jsInner.length();i++) {
     JSONObject jo = new JSONObject(jsInner.get(i).toString());
     hashOut.put((String)jo.get("id"), (String)jo.get("caption"));
   }
   System.out.println(hashOut);
 }

代码示例来源:origin: stackoverflow.com

final JSONParser jParser = new JSONParser();
final JSONObject jObject = jParser.getJSONFromUrl(url);
Iterator<?> keys = jObject.keys();

while(keys.hasNext()){
  final String key = (String)keys.next();
  final Object obj = jObject.get(key);
  if(obj instanceof JSONObject){
    final String rate = jObject.getString(key);
    System.out.println(key + " => " + rate);
    list.add(rate);
  }
}

代码示例来源:origin: loklak/loklak_server

public static String checkMessageExistence(JSONObject message) {
  String source_type = (String) message.get("source_type");
  JSONArray location_point = message.getJSONArray("location_point");
  Double latitude = (Double) location_point.get(0);
  Double longitude = (Double) location_point.get(1);
  String query = "/source_type=" + source_type + " /location=" + latitude + "," + longitude;
  // search only latest message
  DAO.SearchLocalMessages search = new DAO.SearchLocalMessages(query, Order.CREATED_AT, 0, 1, 0);
  Iterator<TwitterTweet> it = search.timeline.iterator();
  while (it.hasNext()) {
    TwitterTweet messageEntry = it.next();
    if (compareMessage(messageEntry.toJSON(), message)) {
      return messageEntry.getPostId();
    }
  }
  return null;
}

代码示例来源:origin: zzz40500/GsonFormat

while (iterator.hasNext()) {
  String name = iterator.next();
  Object valueThis = this.get(name);
  Object valueOther = ((JSONObject)other).get(name);
  if (valueThis instanceof JSONObject) {
    if (!((JSONObject)valueThis).similar(valueOther)) {

代码示例来源:origin: facebook/jcommon

JSONObject expanded = new JSONObject();
while (!toTraverse.isEmpty()) {
 String current = toTraverse.remove();
 JSONObject json = load(current);
 JSONObject conf = json.getJSONObject(CONF_KEY);
 Iterator<String> iter = conf.keys();
 while (iter.hasNext()) {
  String key = iter.next();
   expanded.put(key, conf.get(key));
  JSONArray includes = json.getJSONArray(INCLUDES_KEY);
  for (int idx = 0; idx < includes.length(); idx++) {
   String include = resolve(current, includes.getString(idx));
   if (traversedFiles.contains(include)) {

代码示例来源:origin: stackoverflow.com

JSONArray a = (JSONArray) parser.parse(new FileReader("c:\\exer4-courses.json"));
for (Object o : a)
{
 JSONObject person = (JSONObject) o;
 String name = (String) person.get("name");
 System.out.println(name);
 String city = (String) person.get("city");
 System.out.println(city);
 String job = (String) person.get("job");
 System.out.println(job);
 JSONArray cars = (JSONArray) jsonObject.get("cars");
 for (Object c : cars)
 {
  System.out.println(c+"");
 }
}

代码示例来源:origin: stackoverflow.com

JSONObject obj = (JSONObject) new JSONParser().parse(data);
System.out.println(obj);
JSONObject userList = (JSONObject) obj.get("userList");
JSONArray user = (JSONArray) userList.get("user");
JSONObject userObj = (JSONObject) user.get(0);
String customerId = (String) userObj.get("customerId");
System.out.println("Check Data " + customerId);

代码示例来源:origin: apache/hive

JSONArray conditionMap = opObject.getJSONArray("condition map:");
for (int index = 0; index < conditionMap.length(); index++) {
 JSONObject cond = conditionMap.getJSONObject(index);
 String k = (String) cond.keys().next();
 JSONObject condObject = new JSONObject((String)cond.get(k));
 String type = condObject.getString("type");
 String left = condObject.getString("left");
 String right = condObject.getString("right");
 if (keys.length() != 0) {
  sb.append(posToOpId.get(left) + "." + keys.get(left) + "=" + posToOpId.get(right) + "."
    + keys.get(right) + "(" + type + "),");
 } else {
JSONArray conditionMap = opObject.getJSONArray("condition map:");
for (int index = 0; index < conditionMap.length(); index++) {
 JSONObject cond = conditionMap.getJSONObject(index);
 String k = (String) cond.keys().next();
 JSONObject condObject = new JSONObject((String)cond.get(k));
 String type = condObject.getString("type");
 String left = condObject.getString("left");
 String right = condObject.getString("right");
 if (keys.length() != 0) {
  sb.append(posToOpId.get(left) + "." + keys.get(left) + "=" + posToOpId.get(right) + "."
    + keys.get(right) + "(" + type + "),");
 } else {

代码示例来源:origin: loklak/loklak_server

if (this.selectionMapping != null && this.selectionMapping.size() == 1) {
  final String aggregator = this.selectionMapping.keySet().iterator().next();
  final String aggregator_as = this.selectionMapping.get(aggregator);
  if (aggregator.startsWith("COUNT(") && aggregator.endsWith(")")) { // TODO: there should be a special pattern for this to make it more efficient
    return a.put(new JSONObject().put(aggregator_as, choices.length()));
    return a.put(new JSONObject().put(aggregator_as, max.get()));
    return a.put(new JSONObject().put(aggregator_as, min.get()));
    final AtomicDouble sum = new AtomicDouble(0.0d); String c = aggregator.substring(4, aggregator.length() - 1);
    choices.forEach(json -> sum.addAndGet(((JSONObject) json).getDouble(c)));
    return a.put(new JSONObject().put(aggregator_as, sum.get() / choices.length()));
  String aggregator = ci.next(); String column = ci.next();
  if (column.indexOf('(') >= 0) {String s = aggregator; aggregator = column; column = s;}
  final String aggregator_as = this.selectionMapping.get(aggregator);
    choices.forEach(json -> a.put(new JSONObject()
        .put(aggregator_as, 100.0d * ((JSONObject) json).getDouble(c) / sum.get())
        .put(column_as, ((JSONObject) json).get(column_final))));
    return a;

代码示例来源:origin: apache/hive

JSONObject json = jsonOutput ? new JSONObject(new LinkedHashMap<>()) : null;
  if (this.work != null && (this.work.isUserLevelExplain() || this.work.isFormatted())) {
   if (jsonOut != null && jsonOut.length() > 0) {
    ((JSONObject) jsonOut.get(JSONObject.getNames(jsonOut)[0])).put("OperatorId:",
      operator.getOperatorId());
    ((JSONObject) jsonOut.get(JSONObject.getNames(jsonOut)[0])).put("outputname:",
      ((ReduceSinkDesc) operator.getConf()).getOutputName());
    JSONObject jsonOut = outputPlan(op, out, extended, jsonOutput, cindent, "", inTest);
    if (jsonOutput) {
     ((JSONObject)json.get(JSONObject.getNames(json)[0])).accumulate("children", jsonOut);
      out.print(" ");
     out.println(val);
     } else {
      for(String k: JSONObject.getNames(jsonOut)) {
       json.put(k, jsonOut.get(k));
  JSONObject ret = new JSONObject(new LinkedHashMap<>());
  ret.put(keyJSONObject, json);
  return ret;

代码示例来源:origin: stackoverflow.com

JSONObject jsonObject = new JSONObject(jsonString);
JSONObject myResponse = jsonObject.getJSONObject("MyResponse");
JSONArray tsmresponse = (JSONArray) myResponse.get("listTsm");
for(int i=0; i<tsmresponse.length(); i++){
  list.add(tsmresponse.getJSONObject(i).getString("name"));
System.out.println(list);

代码示例来源:origin: stackoverflow.com

Object value = source.get(key);
    if (!target.has(key)) {
JSONObject a = new JSONObject("{offer: {issue1: value1}, accept: true}");
JSONObject b = new JSONObject("{offer: {issue2: value2}, reject: false}");
System.out.println(a+ " + " + b+" = "+JsonUtils.deepMerge(a,b));

代码示例来源:origin: facebook/facebook-android-sdk

private User jsonToUser(JSONObject user) throws JSONException {
  Uri picture = Uri.parse(user.getJSONObject("picture").getJSONObject("data").getString
      ("url"));
  String name = user.getString("name");
  String id = user.getString("id");
  String email = null;
  if (user.has("email")) {
    email = user.getString("email");
  }
  // Build permissions display string
  StringBuilder builder = new StringBuilder();
  JSONArray perms = user.getJSONObject("permissions").getJSONArray("data");
  builder.append("Permissions:\n");
  for (int i = 0; i < perms.length(); i++) {
    builder.append(perms.getJSONObject(i).get("permission")).append(": ").append(perms
        .getJSONObject(i).get("status")).append("\n");
  }
  String permissions = builder.toString();
  return new User(picture, name, id, email, permissions);
}

代码示例来源:origin: loklak/loklak_server

@Override
  protected void customProcessing(JSONObject message) {
    JSONObject location = (JSONObject) message.get("position");

    final Double longitude = Double.parseDouble((String) location.get("long"));
    final Double latitude = Double.parseDouble((String) location.get("lat"));
    List<Double> location_point = new ArrayList<>();
    location_point.add(longitude);
    location_point.add(latitude);
    message.put("location_point", location_point);
    message.put("location_mark", location_point);

    JSONObject user = new JSONObject(true);
    user.put("screen_name", "freifunk_" + message.get("name"));
    user.put("name", message.get("name"));
    message.put("user", user);
  }
}

相关文章

微信公众号

最新文章

更多

JSONObject类方法