0

I am trying to convert an AjaxObject.messages to a JSON string and send it out to another function from a JavaScript code.

This message should be received by a C# method.

If I send AjaxObject.messages.tradeSchedule.binShares.length instead of AjaxObject.messages I get the correct value in the C# method.

Now I want to send the entire message instead of just the length of a JSON string.

Is there a way to convert that AjaxObject.messages to a JSON string and send it?

Here is the code:

 private string CreateRequestCompleteListenerScript()
    {
        StringBuilder script = new StringBuilder();
        script.AppendLine("var RequestCompleteListener = function(callback) {");
        script.AppendLine("    var internalCallback = {");
        script.AppendLine("        handleCallback:function(message, params){");
        script.AppendLine("            if (message == null){");
        script.AppendLine("                 callback.HandleNullMessage();");
        script.AppendLine("            } else {");
        script.AppendLine("              callback.HandleRequestComplete(AjaxObject.messages);");
        script.AppendLine("            }");
        script.AppendLine("        }");
        script.AppendLine("    }");
        script.AppendLine("    UpdateAjaxObject.addListener('requestcomplete', internalCallback.handleCallback, internalCallback);");
        script.AppendLine("};");
        return script.ToString();
    }

    //AjaxObject.messages.tradeSchedule.binShares.length

    private void SendJavaScript(string script)
    {
        theBrowser.Document.InvokeScript("eval", new object[] {script});
    }

    [System.Runtime.InteropServices.ComVisible(true)]
    public class CallbackHandler
    {
        public CallbackHandler(VoidHandler requestCompleteAction, VoidHandler nullMessageAction)
        {
            _requestCompleteAction = requestCompleteAction;
            _nullMessageAction = nullMessageAction;
        }

        private VoidHandler _requestCompleteAction;
        private VoidHandler _nullMessageAction;
        private bool _requestCompleted;

        [System.Runtime.InteropServices.DispId(0)]
        public void HandleRequestComplete(object messages)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            string json = js.Serialize(messages);
            _log.Info(json);

            if (!_requestCompleted)
                _requestCompleteAction();
            _requestCompleted = true;
        }

This is how the AjaxObject looks like:

AjaxObject.messages

  • js {System.Web.Script.Serialization.JavaScriptSerializer} System.Web.Script.Serialization.JavaScriptSerializer

    json    "{}"    string
    
  • messages {System.__ComObject} object {System.__ComObject}

  • base {System.__ComObject} System.MarshalByRefObject {System.__ComObject}

  • [System.__ComObject] {System.__ComObject} System.__ComObject
  • Non-Public members
  • Dynamic View Expanding the Dynamic View will get the dynamic members for the object
  • Non-Public members
  • Dynamic View Expanding the Dynamic View will get the dynamic members for the object
user175084
  • 4,550
  • 28
  • 114
  • 169

1 Answers1

1

This question has been asked and answered a number of times (how to convert an object to json in c#) but here you go:

JavaScriptSerializer js = new JavaScriptSerializer();
string json = js.Serialize(obj);

Ref. Convert object to JSON string in C#

Community
  • 1
  • 1
user1477388
  • 20,790
  • 32
  • 144
  • 264
  • Hey, Thanks for the quick reply. My question is not how to convert the object to a Json but how to get the object from the javascript in the first place. – user175084 Feb 20 '14 at 19:49
  • 1
    To get JSON to a C# page (either an MVC controller or a codebehind in webforms) you just need to post it. You can use AJAX if you are trying to send it via javascript. – user1477388 Feb 20 '14 at 19:51
  • like if i use this in the JavaScript "callback.HandleRequestComplete(AjaxObject.messages);" and log what i get in the HandleRequestComplete().. the result is "System._ComObject" whereas i want the values instead. – user175084 Feb 20 '14 at 19:55
  • I am not sure what you are asking. You want to take your object and convert it to JSON and then do what? – user1477388 Feb 20 '14 at 20:12
  • If you look at the image above, there is an AjaxObject and inside that there is messages as highlighted. Now i want all the values in that messages to be converted to a JSON string and passed in the callback.HandleRequestComplete(JSON sting here). I just don't know how to convert AjaxObject.messages to a JSOn string. – user175084 Feb 20 '14 at 20:29
  • 1
    Well, I am showing you how to convert your `AjaxObject.messages` object to a string in my answer. You would use it like this `js.Serialize(AjaxObject.messages)`. I am not sure why this isn't solving your issue. Isn't my code working? – user1477388 Feb 20 '14 at 21:06
  • I tried it what you suggested and this is what i get in the logs "{}" but if i pass AjaxObject.messages.tradeSchedule.binShares.length instead of AjaxObject.messages then i get some value "5". So the issue is why am i getting "{}". – user175084 Feb 20 '14 at 22:39
  • Again, thanks for all the help you are providing. Appreciate it! – user175084 Feb 20 '14 at 22:45
  • 1
    Can you please post your latest code so I can see what you are using. I would like to see where you are converting the object to JSON and then passing it to your callback method, please. – user1477388 Feb 21 '14 at 14:16
  • If you put a breakpoint and debug on line `string json = js.Serialize(messages);` what is the value of `json` after the program executes that line? – user1477388 Feb 21 '14 at 15:00
  • I get "{}" for json and i have posted what i see in the debug above. – user175084 Feb 21 '14 at 15:19
  • 1
    What is the value of `messages` which you are sending to the `Serialize()` method? If it is the object as shown in your panel, then the problem may be recursion, although I think it should be able to do it anyway. – user1477388 Feb 21 '14 at 15:21
  • I have edited my post which shows the value of the messages above. – user175084 Feb 21 '14 at 15:23
  • 1
    Thanks but I am a little unsure why it isn't serializing your object. I recommend trying to use Newtonsoft by installing it via NuGet like http://json.codeplex.com/ and use it as per http://james.newtonking.com/json/help/index.html. If you still have problems, you should delete this question and post a new (smaller, more concise) question just showing your two lines of code where you are trying to serialize and it isn't working. Someone smarter than me may be able to help you better. – user1477388 Feb 21 '14 at 15:29