toString() gives Newtonsoft.Json.JsonTextWriter

May 4, 2010 at 11:23 PM

Hi,

Thank you for such a wonderful library. I am having an issue getting the JSON string.

 

        string json = "";
        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);

        using (JsonWriter jsonWriter = new JsonTextWriter(sw))
        {
            jsonWriter.Formatting = Formatting.Indented;
            jsonWriter.WriteStartObject();
            foreach (ProductModel objModel in GetProductModels())
            {
                jsonWriter.WritePropertyName("id");
                jsonWriter.WriteValue("2");
                jsonWriter.WritePropertyName("name");
                jsonWriter.WriteValue("two");
            }
            jsonWriter.WriteEndObject();
            json = jsonWriter.ToString();
        }
        return json;

 When I try to print json the value I get is "Newtonsoft.Json.JsonTextWriter". How do I get the string representation? Thank you.

Coordinator
May 5, 2010 at 1:59 AM

sb.ToString() or sw.ToString()

May 5, 2010 at 5:26 PM

Thank you.