开发者

Writing Json using Newtonsoft.json.JsonTextWriter

I am writing a json using Newtonsoft.json.JsonTextWriter. Here is my code:

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
JsonWriter jsonWriter = new JsonTextWriter(sw);

jsonWriter.Formatting = Formatting.Indented;

jsonWriter.WritePropertyName("Name");
jsonWriter.WriteValue("Allan");

And i am assuming that sw has the json format {"Name": "Allan"}. How can i get the written text in some string variable so that i can use this json data in my htt开发者_StackOverflowp request?


My answer is now not relevant, since the code sample in the question has been edited to include these lines, left here for posterity, see comments for more info.


You will need to add the following to close the JSON elements properly:

jsonWriter.WriteEndObject();

Then call the StringBuilder's ToString() method:

string strMyString = sb.ToString(); //JSONString

References:

StringWriter Constructor (MSDN) | Reading and Writing JSON (NewtonKing.com)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜