how to validate JSON string before converting to XML in C# -


  1. i receive response in form of json string.
  2. we have existing tool developed in c# take input in xml format.
  3. hence converting json string obtained server using newtonsoft.json xml string , passing tool.

problem: when converting json response xml, getting error

"failed process request. reason: ' ' character, hexadecimal value 0x20, cannot included in name."

the above error indicates json key contains space [for example: \"poi items\":[{\"lat\":{\"value\":\"00\"}] cannot converted xml element.

is there approach identify spaces json key's ["poi items"] , remove spaces in it?

also suggest alternative solution needn't change existing solution?

regards,
sudhir

you can use json.net , replace names while loading json..

jsonserializer ser = new jsonserializer(); var jobj = ser.deserialize(new jreader(new stringreader(json))) jobject;  var newjson = jobj.tostring(newtonsoft.json.formatting.none); 

.

public class jreader : newtonsoft.json.jsontextreader {     public jreader(textreader r) : base(r)     {     }      public override bool read()     {         bool b = base.read();         if (base.currentstate == state.property && ((string)base.value).contains(' '))         {             base.settoken(jsontoken.propertyname,((string)base.value).replace(" ", "_"));         }         return b;     } } 

input : {"poi items":[{"lat":{"value":"00","ab cd":"de fg"}}]}

output: {"poi_items":[{"lat":{"value":"00","ab_cd":"de fg"}}]}


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -