0

I would like to pass JSON object as request to web service like i mentioned below.

Result:{
 "email":"xxxxxxx",
 "password":"xxxxxx",
 "Marks":[
  {
   "mark1":"50",
   "mark2":"70"
  }
],
"firstname":"xxxx",
"lastname":"xxxxx"
}

My code: ...

HttpPost httppost = new HttpPost("My Url");
    httppost.setEntity(new StringEntity(**message**.toString(), "UTF-8"));

Here message should have json object in above format.How could i format JSON object?

Thanks.

1

5 Answers 5

2

If you arer having trouble in implementing above json object at android side then you can construct it like below,

JSONObject message = new JSONObject();

JSONObject mParams = new JSONObject();
mParams.put("email", "xxxx");
mParams.put("password", "xxx");

JSONArray markArray = new JSONArray(); 
JSONObject markObj = new JSONObject();
markObj.put("mark1", "50");
markObj.put("mark2", "70");
markArray.put(markObj);

mParams.put("Marks", markArray);

mParams.put("FirstName", "xxxx");
mParams.put("lastname", "xxxx");

message.put("Result",mParams);

Now in your code

    HttpPost httppost = new HttpPost("My Url");
    httppost.setEntity(new StringEntity(**message**.toString(), "UTF-8"));
Sign up to request clarification or add additional context in comments.

4 Comments

If i use ur code i got unserialized output like {"Result":{"lastname":"xxxx","FirstName":"xxxx","Marks":[{"mark2":"70","mark1":"50"}],"password":"xxx","email":"xxxx"}}
the position of fields are changed.i need to have email,pass,marks,firstname,lastname in this order.And i dont need the open& close bracket before Result & end
Order doesn't matter for the JSON - This by default due to its design structure. Your server side code will detect it properly as it will evaluate it based on the Key-value pair.
My output was considering "Result" as a key that's why it was formatted as a perfect json object. You can verify Json by using jsonlint.com. If json is not in valid format then server side code will not detect it as a json object and will throw an error.
1

You can just send it like a String:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("json", message.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

and

$data = json_decode($json);

Comments

1

First of all, the JSON which you have mentioned above is INVALID.

Now,

Here message should have json object in above format.How could i format JSON object?

=> There are 2 ways to do that:

1) Create a request structure by using JSONObject or JSONArray classes, something like:

JSONObject objRequest = new JSONObject(); objRequest.putString("email","xxxx"); objRequest.putString("password","xxxx");

while setting entity inside HttpPost object, convert it into the String value.

2) Bad way, simple generate a string value with escape sequences, something like:

String strRequest = "{\"email\":\"xxxxxxx\",\"password\":\"xxxxxx\"}";

Comments

0

This may help you..Use GSON library. GSON is a Google library to parse JSON resources. With regards to the Marshalling, it basically means a way to translate data structures (like objects in an OOP language) to JSON... for instance:

// Java object
class Book {
  private String title;
  private String isbn;
  private Set<author> authors;
}

@Entity
class Author {
  private String firstName;
  private String lastName;
}

To...

{title:   "Vocation Createurs",
 isbn:    "2829302680",
 authors: [{firstName: "Barbara", lastName: "Polla"},
           {firstName: "Pascal",  lastName: "Perez"}]} 

Comments

0

You can also use existing libraries like android-json-rpc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.