0

{     "Name”: ”ValidateUserName”,     “Message”: {         ”UserName”: ”p”     } }

How could i make these parameters to json format to dynamically pass the parameters to AsyncHTTPClient post method.

Using below method gives a wrong json output

HashMap<String, String> param = new HashMap<String, String>();
JSONObject jsonvalue =new JSONObject();
         try {
            jsonvalue.put("Name","ValidateUserName");
        } catch (JSONException e1) {

            e1.printStackTrace();
        }
         try {
            jsonvalue.put("Message",param);
        } catch (JSONException e1) {

            e1.printStackTrace();
        }

Output: { "Name":"ValidateUserName", "Message":" { UserName=amsecmobileuser } " }

Could anyone say why the output is not as expected.......

SMS
  • 558
  • 1
  • 9
  • 22

1 Answers1

1

You have to create another JSONObject for Message. try this:

    HashMap<String, String> param = new HashMap<String, String>();
    param.put("username", "p");
    JSONObject jsonvalue = new JSONObject();
    try {
        jsonvalue.put("Name", "ValidateUserName");
    } catch (JSONException e1) {

    e1.printStackTrace();
    }
    try {
        JSONObject messageObj = new JSONObject();
        messageObj.put("UserName", param.get("username"));
        jsonvalue.put("Message", messageObj);
    } catch (JSONException e1) {

        e1.printStackTrace();
    }
Milan Maharjan
  • 4,156
  • 1
  • 21
  • 28
  • But by this method if we have 10 nested loops we should create 10 separate objects.....do we have any 3rd party libraries to convert to correct json format the data – SMS Jun 06 '14 at 09:01
  • i am not sure, but theres this library called Jackson. may be that'll do the trick. – Milan Maharjan Jun 06 '14 at 09:24