简体   繁体   中英

How to convert string into JSONObject?

I have a string with value 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 :

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
ConvertModel convertModel = new Gson().fromJson(reply, ConvertModel .class);

Then you can validate easily

You can simply use JSONObject to achieve this

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

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. Names are unique, non-null strings. Values may be any mix of JSONObject , JSONArray , Strings , Booleans , Integers , Longs , Doubles or 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 -

JsonObject object = new JsonObject();
object.put("email","abc@xyz.com");

If we convert above JsonObject to string then its value would be -

{"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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM