none
invalid XML error or json parse RRS feed

  • Question

  • I am sending a json string or an xml string to a Com object from a C# windows application.

    The Com object is loaded into a Web Page and fires an event that the web page responds to by calling a function that takes the "XML" or "JSON" and attempts to convert it to a Javascript object like this one:

    var FICommand = {

    Command : "None",
            Accession : "None",
    UserName : "Dave",
    Password : "None",
    OldAccession : "None",
    Event : "None",
    MainExam : "None",
    ReportGuid : "None",
    associatedExams : [] 
    };

    Unfortunately Calling:

    var obj = $.parseJSON(args); 
    Results in an object error

    And this give me an XML error.

    var json = $.xml2json(args);

    Thursday, November 13, 2014 5:58 PM

Answers

  • It seems that the named pipes left behind some invisible strings

    "\u0000"

     that JSON could not handle, once I removed them it all worked fine by using:

      

    var json = new JavaScriptSerializer().Serialize(GlobalClass.PACSEvent);



    • Marked as answer by Peter Viau Friday, November 14, 2014 1:40 PM
    • Edited by Peter Viau Friday, November 14, 2014 1:40 PM
    Friday, November 14, 2014 1:40 PM

All replies

  • Found this code and it seems to work.

      DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(EventData));
                MemoryStream ms = new MemoryStream();
                ser.WriteObject(ms, dfgfdg);
                string json = Encoding.Default.GetString(ms.ToArray());
     

    • Marked as answer by Peter Viau Thursday, November 13, 2014 6:40 PM
    • Unmarked as answer by Peter Viau Thursday, November 13, 2014 7:40 PM
    Thursday, November 13, 2014 6:40 PM
  • Well that did not last long.

    When I copied over to another box I get  : "Object Error" when trying to load in the web page with:

    var obj = $.parseJSON(args);

    Thursday, November 13, 2014 7:43 PM
  • Hi,

    For this scenario, you could try to check whether the json format is valid or not. Here is an example which shows the usage of $.parseJSON:

    var obj = jQuery.parseJSON('{ "name": "John","Command" :  "Command",                "Accession" : "Accession"            }');
    alert(obj.Command);

    For more detailed information, you could refer to:

    http://api.jquery.com/jquery.parsejson/

    http://msdn.microsoft.com/en-us/library/ie/cc836466(v=vs.94).aspx

    Regards


    Friday, November 14, 2014 7:02 AM
    Moderator
  • It seems that the named pipes left behind some invisible strings

    "\u0000"

     that JSON could not handle, once I removed them it all worked fine by using:

      

    var json = new JavaScriptSerializer().Serialize(GlobalClass.PACSEvent);



    • Marked as answer by Peter Viau Friday, November 14, 2014 1:40 PM
    • Edited by Peter Viau Friday, November 14, 2014 1:40 PM
    Friday, November 14, 2014 1:40 PM