This project has moved and is read-only. For the latest updates, please go here.

Valid JSON?

Feb 29, 2012 at 2:25 PM
Edited Feb 29, 2012 at 3:11 PM



Just trying to return valid JSON from a WCF 4.0 REST service.

Currently it fails on:

when the url supplied is: 

Parse error on line 1:
{    "CPU": "Intel
Expecting '{', '['


This example is the from the source code JsonWriter jsonWriter = new JsonTextWriter. 

ANy idea how to produce valid JSON that will pass the JsonLint test?


The interface is:

[WebGet(UriTemplate = "/JsonValid", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]        

Message GetValidJSON();


This calls:

 public Message GetValidJSON()       

 return WebOperationContext.Current.CreateTextResponse(JSONResource.jsonValid, "application/json; charset=utf-8", Encoding.UTF8);       


 this string called above -   JSONResource.jsonValid

 public static string jsonValid { get; set; }

is filled by the default constructor on the JSONResource class which calls this method below:

public static string CreateValidJSON()     

    // create string objects           
   StringBuilder sb = new StringBuilder();           
   StringWriter sw = new StringWriter(sb);           
   // create a JsonWriter with the stringwriter           
using (JsonWriter jsonWriter = new JsonTextWriter(sw))           
      // set the formattting            
 jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented;               
 // start writing             
jsonWriter.WriteValue("DVD read/writer");             
jsonWriter.WriteValue("500 gigabyte hard drive");             
jsonWriter.WriteValue("200 gigabype hard drive");             
// close current object/array             
// close object            

            // return results            
return sw.ToString();