[英]How to convert string into JSONObject?
I have a string with value abc@xyz.com.
我有一个值为
abc@xyz.com.
的字符串abc@xyz.com.
I have to pass this value to server like:我必须将此值传递给服务器,例如:
{"email":"abc@xyz.com"}
I am passing value to server like this using okhttp
:我使用
okhttp
将值传递给这样的服务器:
Map<String, String> map = new HashMap<>();
map.put("email", email);
new PostMethodWithProgress(login_url, map, this, new Callback()
{
@Override
public void done(String reply)
{
try
{
JSONObject object = new JSONObject(reply);
if (object.getString("status").equals("200"))
{
//Toast Success Message
}
else
{
//Toast Failure Message
}
}
catch (Exception e)
{
Log.e("ASA", "Error is: " + e);
}
}
}).execute();
How do i do it?我该怎么做?
Use Google Gson convert string to model and model to string easily使用 Google Gson 轻松将字符串转换为模型和模型到字符串
ConvertModel convertModel = new Gson().fromJson(reply, ConvertModel .class);
Then you can validate easily然后您可以轻松验证
You can simply use JSONObject
to achieve this您可以简单地使用
JSONObject
来实现这一点
JSONObject jsonObject = new JSONObject();
jsonObject.put("email", "abc@xyz.com");
String result = jsonObject.toString();
Output:输出:
{"email":"abc@xyz.com"}
easy way use this code to pass jsonobject as string in okhttp简单的方法是使用此代码在 okhttp 中将 jsonobject 作为字符串传递
String jsonString = "";
try {
JSONObject obj = new JSONObject();
obj.put("email", "abc@xyz.com");
obj.put("pwd", "12356");
jsonString = obj.toString();
//out put like this -> {"email":"abc@xyz.com","pwd":"123456"}
Log.d("JsonString__",jsonString);
}catch (Exception e){};
JsonObject
is a modifiable set of name/value mappings. JsonObject
是一组可修改的名称/值映射。 Names are unique, non-null strings.名称是唯一的非空字符串。 Values may be any mix of
JSONObject
, JSONArray
, Strings
, Booleans
, Integers
, Longs
, Doubles
or NULL
.值可以是
JSONObject
、 JSONArray
、 Strings
、 Booleans
、 Integers
、 Longs
、 Doubles
或NULL
任意组合。
for your case key
is email and value
is abc@xyz.com so as I told JsonObject
we can put both key and value pair like below -对于您的案例,
key
是电子邮件, value
是abc@xyz.com所以正如我告诉JsonObject
我们可以像下面这样放置键和值对 -
JsonObject object = new JsonObject();
object.put("email","abc@xyz.com");
If we convert above JsonObject
to string then its value would be -如果我们将上面的
JsonObject
转换为字符串,那么它的值将是 -
{"email":"abc@xyz.com"}
hope this will help you.希望这会帮助你。
try out this code with your data.用你的数据试试这个代码。
/**
* This method is used to create text size and color code json and store it in json object.
*
* @param textSize text size entered into edit text.
* @param colorOfPreview text color of custom text color.
* @return return json object of created text size and color.
*/
private String createJSONObject(String textSize, int colorOfPreview) {
JSONObject jsonObject = new JSONObject();
try {
// put your values here
jsonObject.put("textSize", textSize);
jsonObject.put("textColor", colorOfPreview);
return jsonObject.toString();
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject.toString();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.