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

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

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

JSONObject.toString介绍

[英]Encodes this object as a compact JSON string, such as:

{"query":"Pizza","locations":[94043,90210]}

[中]将此对象编码为压缩JSON字符串,例如:

{"query":"Pizza","locations":[94043,90210]}

代码示例

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 Builder.
 * @param urlDevice the UrlDevice to clone.
 */
public Builder(UrlDevice urlDevice) {
 mNewId = urlDevice.mId;
 mNewUrl = urlDevice.mUrl;
 mNewExtraData = new JSONObject(urlDevice.mExtraData.toString());
}

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

JSONObject jsonObj = null;
try {
  jsonObj = XML.toJSONObject(sampleXml);
} catch (JSONException e) {
  Log.e("JSON exception", e.getMessage());
  e.printStackTrace();
} 

Log.d("XML", sampleXml);

Log.d("JSON", jsonObj.toString());

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

try {
     JSONObject parent = new JSONObject();
     JSONObject jsonObject = new JSONObject();
     JSONArray jsonArray = new JSONArray();
     jsonArray.put("lv1");
     jsonArray.put("lv2");
     jsonObject.put("mk1", "mv1");
     jsonObject.put("mk2", jsonArray);
     parent.put("k2", jsonObject);
     Log.d("output", parent.toString(2));
   } catch (JSONException e) {
     e.printStackTrace();
   }

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

@Override
public void print(JSONObject inputObject, PrintStream outputStream) throws Exception {
 LOG.info("JsonParser is parsing:" + inputObject.toString());
 this.rewriteObject = outputStream == null;
  printer.println(inputObject.getString("cboInfo"));
  printer.println();
 outputStream.println(printer.toString());

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

String mParameters[] = { "JAK", "1999" };
   JSONObject mJson = new JSONObject();
   try {
     mJson.put("name", "Katy");
     JSONArray mJSONArray = new JSONArray(Arrays.asList(mParameters));
     mJson.putOpt("parameters", mJSONArray);
     mJson.put("Age", 25);
     System.out.println("JSon::"+ mJson.toString());
   } catch (JSONException e) {
     e.printStackTrace();
   }

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

public TelegramApiRequestException(String message, JSONObject object) {
  super(message);
  apiResponse = object.getString(ERRORDESCRIPTIONFIELD);
  errorCode = object.getInt(ERRORCODEFIELD);
  if (object.has(PARAMETERSFIELD)) {
    try {
      parameters = OBJECT_MAPPER.readValue(object.getJSONObject(PARAMETERSFIELD).toString(), ResponseParameters.class);
    } catch (IOException e) {
      BotLogger.severe("APIEXCEPTION", e);
    }
  }
}

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

"/admin/v1/journalcontexts/" + jobId, null, null);
 org.json.JSONObject jsonBody = new JSONObject();
   result = getHttpRequest(jsonBody.toString(), agentUri, _sessionid);
   JSONObject response = new JSONObject(result);
   if(response != null ) {
     s_logger.debug("Job Status result for ["+jobId + "]:: " + result + " Tick and currentTime :" +  System.currentTimeMillis() +" -" + startTick + "job cmd timeout :" +_nccCmdTimeout);
     String status = response.getJSONObject("journalcontext").getString("status").toUpperCase();
     String message = response.getJSONObject("journalcontext").getString("message");
     s_logger.debug("Job Status Progress Status ["+ jobId + "]:: " + status);
     switch(status) {
 s_logger.error(errMsg, e);
} catch (JSONException e) {
 e.printStackTrace();

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

/**
 * Verfies if the signature of a JSONObject is valid
 * @param obj the JSONObject
 * @param key the public key of the signature issuer
 * @return true if the signature is valid
 * @throws SignatureException if the JSONObject does not have a signature or something with the JSONObject is bogus
 * @throws InvalidKeyException if the key is not valid (for example not RSA)
 */
public static boolean verify(JSONObject obj, PublicKey key) throws SignatureException, InvalidKeyException {
  if(!obj.has(signatureString)) throw new SignatureException("No signature supplied");
  Signature signature;
  try {
    signature = Signature.getInstance("SHA256withRSA");
  } catch (NoSuchAlgorithmException e) {
    return false; //does not happen
  }
  String sigString = obj.getString(signatureString);
  byte[] sig = Base64.getDecoder().decode(sigString);
  obj.remove(signatureString);
  signature.initVerify(key);
  signature.update(obj.toString().getBytes(StandardCharsets.UTF_8));
  boolean res = signature.verify(sig);
  obj.put(signatureString, sigString);
  return res;
}

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

GraphObject graphObject = response.getGraphObject();
JSONObject jsonObject = graphObject.getInnerJSONObject();
Log.d("data", jsonObject.toString(0));
  Log.d("uid",friend.getString("uid"));
  Log.d("name", friend.getString("name"));
  Log.d("pic_square",friend.getString("pic_square"));             
e.printStackTrace();

代码示例来源:origin: Bigkoo/Android-PickerView

public ArrayList<JsonBean> parseData(String result) {//Gson 解析
  ArrayList<JsonBean> detail = new ArrayList<>();
  try {
    JSONArray data = new JSONArray(result);
    Gson gson = new Gson();
    for (int i = 0; i < data.length(); i++) {
      JsonBean entity = gson.fromJson(data.optJSONObject(i).toString(), JsonBean.class);
      detail.add(entity);
    }
  } catch (Exception e) {
    e.printStackTrace();
    mHandler.sendEmptyMessage(MSG_LOAD_FAILED);
  }
  return detail;
}

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

ArrayList<String> stringArray = new ArrayList<String>();
JSONArray jsonArray = new JSONArray();
for(int i = 0, count = jsonArray.length(); i< count; i++)
{
  try {
    JSONObject jsonObject = jsonArray.getJSONObject(i);
    stringArray.add(jsonObject.toString());
  }
  catch (JSONException e) {
    e.printStackTrace();
  }
}

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

String message;
JSONObject json = new JSONObject();
json.put("name", "student");

JSONArray array = new JSONArray();
JSONObject item = new JSONObject();
item.put("information", "test");
item.put("id", 3);
item.put("name", "course1");
array.put(item);

json.put("course", array);

message = json.toString();

// message
// {"course":[{"id":3,"information":"test","name":"course1"}],"name":"student"}

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

JSONObject jsonParams = new JSONObject();
jsonParams.put("notes", "Test api support");
StringEntity entity = new StringEntity(jsonParams.toString());
client.post(context, restApiUrl, entity, "application/json",
  responseHandler);

代码示例来源:origin: GitLqr/LQRWeChat

public byte[] encode() {
  JSONObject var1 = new JSONObject();
  try {
    if (!TextUtils.isEmpty(this.getContact_id())) {
      var1.put("contact_id", this.contact_id);
    }
    if (this.getJSONUserInfo() != null) {
      var1.put("bribery", this.getJSONUserInfo());
    }
  } catch (JSONException var4) {
    var4.printStackTrace();
  }
  try {
    return var1.toString().getBytes("UTF-8");
  } catch (UnsupportedEncodingException var3) {
    var3.printStackTrace();
    return null;
  }
}

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

String json = {"phonetype":"N95","cat":"WP"};

try {

  JSONObject obj = new JSONObject(json);

  Log.d("My App", obj.toString());

} catch (Throwable t) {
  Log.e("My App", "Could not parse malformed JSON: \"" + json + "\"");
}

代码示例来源:origin: JessYanCoding/MVPArms

/**
 * json 格式化
 *
 * @param json
 * @return
 */
public static String jsonFormat(String json) {
  if (TextUtils.isEmpty(json)) {
    return "Empty/Null json content";
  }
  String message;
  try {
    json = json.trim();
    if (json.startsWith("{")) {
      JSONObject jsonObject = new JSONObject(json);
      message = jsonObject.toString(4);
    } else if (json.startsWith("[")) {
      JSONArray jsonArray = new JSONArray(json);
      message = jsonArray.toString(4);
    } else {
      message = json;
    }
  } catch (JSONException e) {
    message = json;
  }
  return message;
}

相关文章

JSONObject类方法