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

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

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

JSONObject.<init>介绍

[英]Creates a JSONObject with no name/value mappings.
[中]创建没有名称/值映射的JSONObject。

代码示例

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: google/physical-web

/**
 * Construct a UrlDevice.
 * @param id The id of the device.
 * @param url The URL broadcasted by the device.
 * @param extraData Extra data to associate with this UrlDevice.
 */
private UrlDevice(String id, String url, JSONObject extraData) {
 mId = id;
 mUrl = url;
 mExtraData = extraData == null ? new JSONObject() : new JSONObject(extraData.toString());
}

代码示例来源: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: stackoverflow.com

public boolean isJSONValid(String test) {
  try {
    new JSONObject(test);
  } catch (JSONException ex) {
    // edited, to include @Arthur's comment
    // e.g. in case JSONArray is valid as well...
    try {
      new JSONArray(test);
    } catch (JSONException ex1) {
      return false;
    }
  }
  return true;
}

代码示例来源:origin: bumptech/glide

List<Photo> parse(String response) throws JSONException {
  JSONObject searchResults =
    new JSONObject(response.substring(FLICKR_API_PREFIX_LENGTH, response.length() - 1));
  JSONArray photos = searchResults.getJSONObject("photos").getJSONArray("photo");
  List<Photo> results = new ArrayList<>(photos.length());
  for (int i = 0, size = photos.length(); i < size; i++) {
   results.add(new Photo(photos.getJSONObject(i)));
  }

  return results;
 }
}

代码示例来源: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: RipMeApp/ripme

JSONObject result = new JSONObject();
result.put("photos", new JSONArray());
JSONArray jsonGalleries = json.getJSONArray("galleries");
for (int i = 0; i < jsonGalleries.length(); i++) {
  if (i > 0) {
    sleep(500);
  JSONObject jsonGallery = jsonGalleries.getJSONObject(i);
  long galleryID = jsonGallery.getLong("id");
  String userID = Long.toString(jsonGallery.getLong("user_id"));
  sendUpdate(STATUS.LOADING_RESOURCE, "Gallery ID " + galleryID + " for userID " + userID);
  JSONObject thisJSON = Http.url(blogURL).getJSON();
  JSONArray thisPhotos = thisJSON.getJSONArray("photos");
  for (int j = 0; j < thisPhotos.length(); j++) {
    result.getJSONArray("photos").put(thisPhotos.getJSONObject(j));
JSONObject result = new JSONObject();
result.put("photos", new JSONArray());
  for (int j = 0; j < thisPhotos.length(); j++) {
    result.getJSONArray("photos").put(thisPhotos.getJSONObject(j));

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

JSONObject obj = new JSONObject("{\"name\": \"John\"}");

System.out.println(obj.getString("name")); //John

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

private static JSONObject handleGeneric(JSONArray links, String service, String noVideoMessage) {
  JSONObject ret = new JSONObject(true);
  ret.put("service", service);
  if (links.length() > 0) {
    ret.put("status", 0);
    ret.put("message", "OK");
    ret.put("links", links);
  } else {
    ret.put("status", 2);
    ret.put("message", noVideoMessage);
  }
  return ret;
}

代码示例来源: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

private synchronized static JSONObject parseAppGateKeepersFromJSON(
    final String applicationId,
    JSONObject gateKeepersJSON) {
  JSONObject result;
  if (fetchedAppGateKeepers.containsKey(applicationId)) {
    result = fetchedAppGateKeepers.get(applicationId);
  } else {
    result = new JSONObject();
  }
  JSONArray arr = gateKeepersJSON.optJSONArray(APPLICATION_GRAPH_DATA);
  JSONObject gateKeepers = null;
  if (arr != null) {
    gateKeepers = arr.optJSONObject(0);
  }
  // If there does exist a valid JSON object in arr, initialize result with this JSON object
  if (gateKeepers != null && gateKeepers.optJSONArray(APPLICATION_GATEKEEPER_FIELD) != null) {
    JSONArray data = gateKeepers.optJSONArray(APPLICATION_GATEKEEPER_FIELD);
    for (int i = 0; i < data.length(); i++) {
      try {
        JSONObject gk = data.getJSONObject(i);
        result.put(gk.getString("key"), gk.getBoolean("value"));
      } catch (JSONException je) {
        Utility.logd(Utility.LOG_TAG, je);
      }
    }
  }
  fetchedAppGateKeepers.put(applicationId, result);
  return result;
}

代码示例来源:origin: Vedenin/useful-java-links

public static void main(String[] args) {
    // convert Java to json
    JSONObject root = new JSONObject();
    root.put("message", "Hi");
    JSONObject place = new JSONObject();
    place.put("name", "World!");
    root.put("place", place);
    String json = root.toString();
    System.out.println(json);

    System.out.println();
    // convert json to Java
    JSONObject jsonObject = new JSONObject(json);
    String message = jsonObject.getString("message");
    String name = jsonObject.getJSONObject("place").getString("name");
    System.out.println(message + " " + name);
  }
}

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

/**
 * If during thinking we observe something that we want to memorize, we can memorize this here
 * @param featureName the object key
 * @param observation the object value
 * @return the thought
 */
public SusiThought addObservation(String featureName, String observation) {
  JSONArray data = getData();
  for (int i = 0; i < data.length(); i++) {
    JSONObject spark = data.getJSONObject(i);
    if (!spark.has(featureName)) {
      spark.put(featureName, observation);
      return this;
    }
  }
  data.put(new JSONObject().put(featureName, observation));
  return this;
}

代码示例来源:origin: ankidroid/Anki-Android

/** Copy, save and return. */
public JSONObject copy(JSONObject m) {
  JSONObject m2 = null;
  try {
    m2 = new JSONObject(Utils.jsonToString(m));
    m2.put("name", m2.getString("name") + " copy");
  } catch (JSONException e) {
    throw new RuntimeException(e);
  }
  add(m2);
  return m2;
}

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

private JSONObject parseJSONObject(String jsonStr) {
  if (jsonStr.startsWith("{")) {
    return new JSONObject(jsonStr);
  } else if (jsonStr.startsWith("[")) {
    JSONArray jsonArray = new JSONArray(jsonStr);
    if (jsonArray.length() > 0 && jsonArray.get(0) instanceof JSONObject) {
      return getJsonObject(jsonArray);
    }
  }
  return null;
}

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

@Test
public void testObjectToPrimitive() throws JSONException {
 ArrayOfPrimitivesContainer container = new ArrayOfPrimitivesContainer();
 ArrayList<Object> primitives = container.primitives;
 primitives.add(Long.MIN_VALUE);
 primitives.add(Long.MAX_VALUE);
 primitives.add(Integer.MIN_VALUE);
 primitives.add(Integer.MAX_VALUE);
 primitives.add(Float.MIN_VALUE);
 primitives.add(Float.MAX_VALUE);
 primitives.add(Double.MIN_VALUE);
 primitives.add(Double.MAX_VALUE);
 String json = mObjectMapper.convertValue(container, JSONObject.class).toString();
 JSONObject obj = new JSONObject(json);
 JSONArray array = obj.getJSONArray("primitives");
 ArrayList<Object> actual = new ArrayList<>();
 for (int i = 0, N = array.length(); i < N; i++) {
  actual.add(array.get(i));
 }
 assertEquals(primitives.toString(), actual.toString());
}

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

/**
 * Merging of data is required during an mind-meld.
 * To meld two thoughts, we combine their data arrays into one.
 * The resulting table has the maximum length of the source tables
 * @param table the information to be melted into our existing table.
 * @return the thought
 */
public SusiThought mergeData(JSONArray table1) {
  JSONArray table0 = this.getData();
  while (table0.length() < table1.length()) table0.put(new JSONObject());
  for (int i = 0; i < table1.length(); i++) {
    table0.getJSONObject(i).putAll(table1.getJSONObject(i));
  }
  setData(table0);
  return this;
}

代码示例来源:origin: firebase/firebase-jobdispatcher-android

@NonNull
 private static List<ObservedUri> convertJsonToObservedUris(@NonNull String contentUrisJson) {
  List<ObservedUri> uris = new ArrayList<>();
  try {
   JSONObject json = new JSONObject(contentUrisJson);
   JSONArray jsonFlags = json.getJSONArray(JSON_URI_FLAGS);
   JSONArray jsonUris = json.getJSONArray(JSON_URIS);
   int length = jsonFlags.length();

   for (int i = 0; i < length; i++) {
    int flags = jsonFlags.getInt(i);
    String uri = jsonUris.getString(i);
    uris.add(new ObservedUri(Uri.parse(uri), flags));
   }
  } catch (JSONException e) {
   throw new RuntimeException(e);
  }
  return uris;
 }
}

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

public void actionPerformed(ActionEvent e) {
    String json = editTP.getText();
    json = json.trim();
    if (json.startsWith("{")) {
      JSONObject jsonObject = new JSONObject(json);
      String formatJson = jsonObject.toString(4);
      editTP.setText(formatJson);
    } else if (json.startsWith("[")) {
      JSONArray jsonArray = new JSONArray(json);
      String formatJson = jsonArray.toString(4);
      editTP.setText(formatJson);
    }
  }
});

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

JSONObject json = new JSONObject();
JSONObject manJson = new JSONObject();
manJson.put("name", "emil");
manJson.put("username", "emil111");
manJson.put("age", "111");
json.put("man",manJson);

相关文章

微信公众号

最新文章

更多

JSONObject类方法