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

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

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

JSONObject.getString介绍

[英]Returns the value mapped by name if it exists, coercing it if necessary.
[中]返回按名称映射的值(如果存在),必要时强制该值。

代码示例

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

int id;
String name;
JSONArray array = new JSONArray(string_of_json_array);
for (int i = 0; i < array.length(); i++) {
  JSONObject row = array.getJSONObject(i);
  id = row.getInt("id");
  name = row.getString("name");
}

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

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

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

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

JSONObject json = new JSONObject(jsonString);
JSONArray jArray = json.getJSONArray("list");

System.out.println("*****JARRAY*****" + jArray.length());

for(int i=0; i<jArray.length(); i++){
  JSONObject json_data = jArray.getJSONObject(i);

  Log.i("log_tag", "_id" + json_data.getInt("account") +
    ", mall_name" + json_data.getString("name") +
    ", location" + json_data.getString("number") +
    ", telephone" + json_data.getString("url") +
    ",----" + json_data.getString("balance") +
    ",----" + json_data.getString("credit") +
    ",----" + json_data.getString("displayName")
  );
}

代码示例来源:origin: RipMeApp/ripme

@Override
public List<String> getURLsFromPage(Document doc) {
  List<String> result = new ArrayList<>();
  Elements videos = doc.select("script");
  for (Element el : videos) {
    String json = el.html();
    if (json.startsWith("{")) {
      JSONObject page = new JSONObject(json);
      result.add(page.getJSONObject("video").getString("contentUrl"));
    }
  }
  return result;
}

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

private List<Partition> partitions() throws JSONException, TException {
 if (tableDesc == null) {
  return null;
 }
 // TODO : jackson-streaming-iterable-redo this
 JSONArray jsonPartitions = new JSONArray(json.getString(PartitionSerializer.FIELD_NAME));
 List<Partition> partitionsList = new ArrayList<>(jsonPartitions.length());
 for (int i = 0; i < jsonPartitions.length(); ++i) {
  String partDesc = jsonPartitions.getString(i);
  partitionsList.add(deserialize(new Partition(), partDesc));
 }
 return partitionsList;
}

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

String json = "Assuming that here is your JSON response"; 
try {
  JSONObject parentObject = new JSONObject(json);
  JSONObject userDetails = parentObject.getJSONObject("user_details"); 

  //And then read attributes like             
  String name = userDetails.getString("user_name"); 
  String phone = userDetails.getString("user_phone");
  String id = userDetails.getString("re‌​f_id");

} catch (JSONException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}

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

@Override
public void onResponse(JSONArray response) {
  try {
    JSONObject jsonObject = response.getJSONObject(0);
    firstNameRef.set(jsonObject.getString("firstName"));
    lastNameRef.set(jsonObject.getString("lastName"));
    latch.countDown();
  } catch (JSONException e) {
    assertTrue(false);
  }
}

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

Bundle bundle = getBundleFromIntentOrWhaterver();
JSONObject json = null;
try {
  json = new JSONObject(bundle.getString("json"));
  String key = json.getString("key");
} catch (JSONException e) {
  e.printStackTrace();
}

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

JSONObject obj = new JSONObject(jsonString);
 String id = obj.getString("id");
 String error = obj.getString("error");
 JSONObject result = obj.getJSONObject("result");
 int nPeople = result.getInt("nPeople");
 JSONArray people = result.getJSONArray("people");
 for(int i = 0 ; i < people.length() ; i++){
   JSONObject p = (JSONObject)people.get(i);
   String namePeople = p.getString("namePeople");
   ...
 }

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

@Override
public CharSequence getPageTitle(int position) {
  try {
    return mModel.getJSONArray("tmpls").getJSONObject(position).getString("name");
  } catch (JSONException e) {
    throw new RuntimeException(e);
  }
}

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

JSONObject ret = getLocationInfo(); 
JSONObject location;
String location_string;
try {
  location = ret.getJSONArray("results").getJSONObject(0);
  location_string = location.getString("formatted_address");
  Log.d("test", "formattted address:" + location_string);
} catch (JSONException e1) {
  e1.printStackTrace();

}

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

private CommandMode readMode(JSONObject jsonObject) throws JSONException {
 CommandMode mode = new CommandMode();
 mode.name = jsonObject.getString("name");
 mode.text = jsonObject.getString("text");
 mode.leadOption = jsonObject.getString("lead-option");
 mode.options = toStringArray(jsonObject.getJSONArray("options"));
 return mode;
}

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

public static void main(String[] args) throws Exception {
  String jsonStr = "{         \"dataArray\": [{              \"A\": \"a\",                \"B\": \"b\",               \"C\": \"c\"            }, {                \"A\": \"a1\",              \"B\": \"b2\",              \"C\": \"c3\"           }]      }";

  JSONObject jsonObj = new JSONObject(jsonStr);

  JSONArray c = jsonObj.getJSONArray("dataArray");
  for (int i = 0 ; i < c.length(); i++) {
    JSONObject obj = c.getJSONObject(i);
    String A = obj.getString("A");
    String B = obj.getString("B");
    String C = obj.getString("C");
    System.out.println(A + " " + B + " " + C);
  }
}

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

Object response = ((JavascriptExecutor) driver).executeAsyncScript(
   "var callback = arguments[arguments.length - 1];" +
   "var xhr = new XMLHttpRequest();" +
   "xhr.open('GET', '/resource/data.json', true);" +
   "xhr.onreadystatechange = function() {" +
   "  if (xhr.readyState == 4) {" +
   "    callback(xhr.responseText);" +
   "  }" +
   "}" +
   "xhr.send();");
 JSONObject json = new JSONObject((String) response);
 assertEquals("cheese", json.getString("food"));

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

JSONArray jsonarray = new JSONArray(jsonStr);
for (int i = 0; i < jsonarray.length(); i++) {
  JSONObject jsonobject = jsonarray.getJSONObject(i);
  String name = jsonobject.getString("name");
  String url = jsonobject.getString("url");
}

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

public static Message toObject(String jsonStr) {
  Message m =  new Message();
  try {
    JSONObject jsonObject = new JSONObject(jsonStr);
    m.setHandlerName(jsonObject.has(HANDLER_NAME_STR) ? jsonObject.getString(HANDLER_NAME_STR):null);
    m.setCallbackId(jsonObject.has(CALLBACK_ID_STR) ? jsonObject.getString(CALLBACK_ID_STR):null);
    m.setResponseData(jsonObject.has(RESPONSE_DATA_STR) ? jsonObject.getString(RESPONSE_DATA_STR):null);
    m.setResponseId(jsonObject.has(RESPONSE_ID_STR) ? jsonObject.getString(RESPONSE_ID_STR):null);
    m.setData(jsonObject.has(DATA_STR) ? jsonObject.getString(DATA_STR):null);
    return m;
  } catch (JSONException e) {
    e.printStackTrace();
  }
  return m;
}

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

@Override
public void onNext(JSONArray response) {
  try {
    JSONObject jsonObject = response.getJSONObject(0);
    firstNameRef.set(jsonObject.getString("firstName"));
    lastNameRef.set(jsonObject.getString("lastName"));
    latch.countDown();
  } catch (JSONException e) {
    assertTrue(false);
  }
}

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

try {
  JSONObject jsonObject = new JSONObject(theJsonString);
  Iterator keys = jsonObject.keys();
  Map<String, String> map = new HashMap<String, String>();
  while (keys.hasNext()) {
    String key = (String) keys.next();
    map.put(key, jsonObject.getString(key));
  }
  System.out.println(map);// this map will contain your json stuff
} catch (JSONException e) {
  e.printStackTrace();
}

代码示例来源:origin: RipMeApp/ripme

public String getAfter(JSONObject json) {
  try {
    return json.getJSONObject("entry_data").getJSONArray("ProfilePage").getJSONObject(0)
        .getJSONObject("graphql").getJSONObject("user")
        .getJSONObject("edge_owner_to_timeline_media").getJSONObject("page_info").getString("end_cursor");
  } catch (JSONException e) {
    // This is here so that when the user rips the last page they don't get a "end_cursor not a string" error
    try {
      return json.getJSONObject("data").getJSONObject("user")
          .getJSONObject("edge_owner_to_timeline_media").getJSONObject("page_info").getString("end_cursor");
    } catch (JSONException t) {
      return "";
    }
  }
}

相关文章

JSONObject类方法