开发者

.NET Append JSON to a string already serialized with DataContractJsonSerializer

I am using DataContractJsonSerializer to serialize an object to json, and then returning to the client with AJAX. I now need to serialize another object to return along with it. The problem is MSs "d" that wraps around the JSON, that stops me from simply concatenating the strings into a single JSON string.

json = json & """,""SecurityGroups"": 1"

Returns:

{
    "d":"[{
        \"__type\":\"U开发者_开发知识库ser:#HagarDB\",
        \"ID\":1
    }]\",
    \"SecurityGroup\": 1"
}

Any suggestions would be greatly appreciated. I'd rather not have to make another call to the server to get the other object.


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; }
}


As RPM1984 suggests in the comment, the best solution would probably be to create a class that contains both the objects that you want to serialize.

If that for some reason isn't an option then you could do some simple string manipulation to insert the new object:

// Find position of the ending characters }] 
var endPosition = json.IndexOf("}]");
// Insert the new json between the } and the ]
var newJson = json.Substring(0, endPosition +1) + newJson + json.Substring(endPosition);

Maybe you need to add a comma before the new json, and maybe you need to tweak the string indexes, but you get the point. Since json is a simple string format, you can treat it as a string as well.

This last sugggestion is quite ugly but it should work as a one-off solution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜