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

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

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

JSONObject.put介绍

[英]Maps name to value, clobbering any existing name/value mapping with the same name.
[中]将名称映射到值,用相同的名称替换任何现有的名称/值映射。

代码示例

canonical example by Tabnine

public String creatingJsonString() {
  JSONArray pets = new JSONArray();
  pets.put("cat");
  pets.put("dog");
  JSONObject person = new JSONObject();
  person.put("name", "John Brown");
  person.put("age", 35);
  person.put("pets", pets);
  return person.toString(2);
}

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

JSONObject jo = new JSONObject();
jo.put("firstName", "John");
jo.put("lastName", "Doe");

JSONArray ja = new JSONArray();
ja.put(jo);

JSONObject mainObj = new JSONObject();
mainObj.put("employees", ja);

代码示例来源:origin: google/physical-web

/**
 * Create a JSON object that represents this data structure.
 * @return a JSON serialization of this data structure.
 */
public JSONObject jsonSerialize() {
 JSONObject jsonObject = new JSONObject();
 jsonObject.put(ID_KEY, mId);
 jsonObject.put(URL_KEY, mUrl);
 if (mExtraData.length() > 0) {
  jsonObject.put(EXTRA_KEY, mExtraData);
 }
 return jsonObject;
}

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

JSONObject cred = new JSONObject();
JSONObject auth=new JSONObject();
JSONObject parent=new JSONObject();
cred.put("username","adm");
cred.put("password", "pwd");
auth.put("tenantName", "adm");
auth.put("passwordCredentials", cred);
parent.put("auth", auth);

OutputStreamWriter wr= new OutputStreamWriter(con.getOutputStream());
wr.write(parent.toString());

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

public JSONObject outputDependencies(PrintStream out, boolean jsonOutput,
  boolean appendTaskType, List<Task> tasks)
  throws Exception {
 if (out != null) {
  out.println(STAGE_DEPENDENCIES + ":");
 }
 JSONObject json = jsonOutput ? new JSONObject(new LinkedHashMap<>()) : null;
 for (Task task : tasks) {
  JSONObject jsonOut = outputDependencies(task, out, json, jsonOutput, appendTaskType, 2);
  if (jsonOutput && jsonOut != null) {
   json.put(task.getId(), jsonOut);
  }
 }
 return jsonOutput ? json : null;
}

代码示例来源:origin: lzyzsd/JsBridge

public String toJson() {
  JSONObject jsonObject= new JSONObject();
  try {
    jsonObject.put(CALLBACK_ID_STR, getCallbackId());
    jsonObject.put(DATA_STR, getData());
    jsonObject.put(HANDLER_NAME_STR, getHandlerName());
    String data = getResponseData();
    if (TextUtils.isEmpty(data)) {
     jsonObject.put(RESPONSE_DATA_STR, data);
    } else {
     jsonObject.put(RESPONSE_DATA_STR, new JSONTokener(data).nextValue());
    }
    jsonObject.put(RESPONSE_DATA_STR, getResponseData());
    jsonObject.put(RESPONSE_ID_STR, getResponseId());
    return jsonObject.toString();
  } catch (JSONException e) {
    e.printStackTrace();
  }
  return null;
}

代码示例来源:origin: alibaba/Tangram-Android

private void addCardStyle(JSONObject cardData, Card card) {
  try {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Float.class, new JsonSerializer<Float>() {
      @Override
      public JsonElement serialize(final Float src, final Type typeOfSrc, final JsonSerializationContext context) {
        try {
          if (src.isInfinite() || src.isNaN()) {
            return new JsonPrimitive(0f);
          }
          BigDecimal value = BigDecimal.valueOf(src);
          return new JsonPrimitive(value);
        } catch (Exception e) {
          e.printStackTrace();
        }
        return new JsonPrimitive(0f);
      }
    });
    Gson gson = gsonBuilder.create();
    GridCard.GridStyle gridStyle = new GridCard.GridStyle();
    if (card instanceof BannerCard) {
      gridStyle.aspectRatio = 3.223f;
    }
    cardData.put(Card.KEY_STYLE, new JSONObject(gson.toJson(gridStyle)));
  } catch (JSONException e) {
    e.printStackTrace();
  }
}

代码示例来源:origin: ACRA/acra

private void collectCurrentSizeRange(@NonNull Display display, @NonNull JSONObject container) throws JSONException {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    final Point smallest = new Point();
    final Point largest = new Point();
    display.getCurrentSizeRange(smallest, largest);
    final JSONObject result = new JSONObject();
    result.put("smallest", new JSONArray(Arrays.asList(smallest.x, smallest.y)));
    result.put("largest", new JSONArray(Arrays.asList(largest.x, largest.y)));
    container.put("currentSizeRange", result);
  }
}

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

public void setOnJSON(JSONObject json, String key, Object value)  throws JSONException {
    JSONArray jsonArray = new JSONArray();
    for (String stringValue : (String[])value) {
      jsonArray.put(stringValue);
    }
    json.put(key, jsonArray);
  }
});

代码示例来源:origin: ACRA/acra

private void collectSize(@NonNull Display display, @NonNull JSONObject container) throws JSONException {
  final Point size = new Point();
  display.getSize(size);
  container.put("size", new JSONArray(Arrays.asList(size.x, size.y)));
}

代码示例来源:origin: alibaba/Tangram-Android

private void transformCardCellData(JSONObject cardData) {
  try {
    if (cardData.has("iconList")) {
      cardData.put(Card.KEY_ITEMS, cardData.getJSONArray("iconList"));
    } else if (cardData.has("centerBannerList")) {
      cardData.put(Card.KEY_ITEMS, cardData.getJSONArray("centerBannerList"));
    }
  } catch (JSONException e) {
    e.printStackTrace();
  }
}

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

public JSONObject outputDependencies(PrintStream out, boolean jsonOutput,
  boolean appendTaskType, List<Task> tasks)
  throws Exception {
 if (out != null) {
  out.println("STAGE DEPENDENCIES:");
 }
 JSONObject json = jsonOutput ? new JSONObject(new LinkedHashMap<>()) : null;
 for (Task task : tasks) {
  JSONObject jsonOut = outputDependencies(task, out, json, jsonOutput, appendTaskType, 2);
  if (jsonOutput && jsonOut != null) {
   json.put(task.getId(), jsonOut);
  }
 }
 return jsonOutput ? json : null;
}

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

JSONObject student1 = new JSONObject();
try {
  student1.put("id", "3");
  student1.put("name", "NAME OF STUDENT");
  student1.put("year", "3rd");
  student1.put("curriculum", "Arts");
  student1.put("birthday", "5/5/1993");
JSONObject student2 = new JSONObject();
try {
  student2.put("id", "2");
  student2.put("name", "NAME OF STUDENT2");
  student2.put("year", "4rd");
  student2.put("curriculum", "scicence");
  student2.put("birthday", "5/5/1993");
JSONArray jsonArray = new JSONArray();
jsonArray.put(student1);
jsonArray.put(student2);
JSONObject studentsObj = new JSONObject();
  studentsObj.put("Students", jsonArray);
String jsonStr = studentsObj.toString();
  System.out.println("jsonString: "+jsonStr);

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

JSONObject cred = new JSONObject();
JSONObject auth=new JSONObject();
JSONObject parent=new JSONObject();
cred.put("username","adm");
cred.put("password", "pwd");
auth.put("tenantName", "adm");
auth.put("passwordCredentials", cred.toString()); // <-- toString()
parent.put("auth", auth.toString());              // <-- toString()

OutputStreamWriter wr= new OutputStreamWriter(con.getOutputStream());
wr.write(parent.toString());

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

RequestQueue requestQueue = Volley.newRequestQueue(this);
String URL = "http://...";
JSONObject jsonBody = new JSONObject();
jsonBody.put("Title", "Android Volley Demo");
jsonBody.put("Author", "BNK");
final String mRequestBody = jsonBody.toString();
e.printStackTrace();

代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking

public void createAnUserJSONObject(View view) {
  JSONObject jsonObject = new JSONObject();
  try {
    jsonObject.put("firstname", "Rohit");
    jsonObject.put("lastname", "Kumar");
  } catch (JSONException e) {
    e.printStackTrace();

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

ArrayList<String> list = new ArrayList<String>();
list.add("john");
list.add("mat");
list.add("jason");
list.add("matthew");

JSONObject school = new JSONObject();

school.put("class","4");
school.put("name", new JSONArray(list));

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

@Override
  public void setOnJSON(JSONObject json, String key, Object value) throws JSONException {
    JSONArray jsonArray = new JSONArray();
    for (String stringValue : (String[]) value) {
      jsonArray.put(stringValue);
    }
    json.put(key, jsonArray);
  }
});

代码示例来源:origin: commonsguy/cw-omnibus

private JSONObject dumpStructure(JSONObject json)
 throws JSONException {
 return (json.put("windows",
  dumpStructureWindows(new JSONArray())));
}

代码示例来源:origin: jwtk/jjwt

private JSONObject toJSONObject(Map<?, ?> m) {
  JSONObject obj = new JSONObject();
  for (Map.Entry<?, ?> entry : m.entrySet()) {
    Object k = entry.getKey();
    Object value = entry.getValue();
    String key = String.valueOf(k);
    value = toJSONInstance(value);
    obj.put(key, value);
  }
  return obj;
}

相关文章

微信公众号

最新文章

更多

JSONObject类方法