Reputation: 4116
I want to post following string as json to the server.
{ "FirstName": "John", "LastName": "Smith" }, { "FirstName": "John", "LastName": "Smith" }
But if I use below code, I am getting array list of json objects.
Gson gson = new Gson();
Type type = new TypeToken<List<Student>>() {}.getType();
String json = gson.toJson(list, type);
Output:
[{ "FirstName": "John", "LastName": "Smith" }, { "FirstName": "John", "LastName": "Smith" }]
So can anybody help me to achieve this ? Or server guy made a mistake ? I want to submit it as a post method of retrofit library.
Upvotes: 1
Views: 627
Reputation: 4116
As stated by Simon, it is not a valid json as I check with JSONLint. So I talked with server guy and changed to valid json like
[{ "FirstName": "John", "LastName": "Smith" }, { "FirstName": "John", "LastName": "Smith" }]
I am posting my answer as it may help to anybody that face such type of issue in future.
Upvotes: 1
Reputation: 77904
I want to post following string as json to the server.
Suppose you use header: "content-type", "application/json"
The string you posted - is wrong JSON format:
{ "FirstName": "John", "LastName": "Smith" }, { "FirstName": "John", "LastName": "Smith" }
Your output is a right JSON syntax a.e.:
[{ "FirstName": "John", "LastName": "Smith" }, { "FirstName": "John", "LastName": "Smith" }]
So use it
Or server guy made a mistake ?
Looks like. I believe he thought about [{},{}]
structure
Anyways if you want to send { "FirstName": "John", "LastName": "Smith" }, { "FirstName": "John", "LastName": "Smith" }
- you have nothing to do with Gson
. You got list of objects, just remove [
, ]
from beginning and end
private static String removeLastChar(String str) {
return str.substring(0, str.length() - 1);
}
public String removeFirstChar(String s){
return s.substring(1);
}
Upvotes: 2