There are at least two roblems in your code. The first one: you make JSON serialization twice. The second: you can not append JSON string with another data because in the result string will be not more in the JSON format.
If you use [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
attribute for the web method, the object which you give back will be automatically serialized to JSON string. So you should not serialize it manually before.
If the object which you want serialize is already a string then during the serialization all quoates will ne esacped ("
will be replaced to \"
). In your case after manual object serialization you received the string [{"__type":"User:#HagarDB", "ID":1}]
which is correct JSON string. To verify this you can just paste the string in validator http://www.jsonlint.com/. More about the JSON format you can read on http://www.json.org/.
If you append the data with another string like "SecurityGroup": 1
(which is not a JSON string, correct will be {"SecurityGroup": 1}
) with a comma between the strings you will receive the string
[{"__type":"User:#HagarDB", "ID":1}], "SecurityGroup": 1
which is also wrong JSON. Correct JSON will be something like
{ "MyArray": [ {"__type": "User:#HagarDB", "ID": 1 } ], "SecurityGroup": 1 }
At the end you return the string as a result of the web methid and receive result in the form {d: result}
where all quotas will be escaped:
{
"d": "[{\"__type\":\"User:#HagarDB\", \"ID\":1}], \"SecurityGroup\": 1"
}
This is a JSON string, but it is not what you want.
The solution of your problem is very simple. You web method can looks like following
[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public MyResult MyMethod () {
List<Users> users = BuildMyInnerInformation();
return new MyResult { Users: users, SecurityGroup: 1};
}
public class MyResult {
public List<Users> Users { get; set; }
public int SecurityGroup { get; set; }
}