سؤال

I'm creating a winmd file for use in Windows 8 development. I want to have a great JavaScript (WinJS) experience but can't work out how to have my methods except raw JSON, for example I would like developers to code like this in WinJS:

bar.foo({ bar: 19 })

And inside my C# library I would have something like this

public sealed class Bar
{
    public void Foo(JsonObject jsonObject)
    {

This compiles but when I try to call foo from WinJS I get an error saying the signature of the method is invalid. I'm assuming this is because it exposes a 'managed' type Windows.Data.Json.JsonObject.

Any ideas how I can work with JSON passed from the WinJS world into .NET (within a WinMD).

هل كانت مفيدة؟

المحلول

I think it's not possible, at least in the Developer Preview.

I created a C# method that has an object parameter with the assumption that any object that can be converted from JS form to .Net form through WinRT will be able to go through.

And it works for arrays: a JS array will be passed in as object[]. But if I try to pass a JSON object, a “Type mismatch” error is thrown. This is why I think it's not possible.

I also tried object created using the WinJS.Class.define() function, but that didn't work either.

نصائح أخرى

This is almost a year old, but for anyone else encountering the same issue...

This can be done, but you need to make your winmd method signature takes in a string as a parameter and then use the static JsonObject.Parse parse the JSON text.

public sealed class Bar
{
    public void Foo(string json)
    {
        if (!String.IsNullOrEmpty(json))
        {
            var jobj = JsonObject.Parse(json);
            var barVal = jobj.GetNamedNumber("bar");
            // if all went well, barVal should be a double value of 
            // the number passed in the object (19.0 based on the original question).
        }                 
    }
}

When you call this method, though, you need to wrap the object definition in quotes to make it a string. You also need to wrap the field names in quotes or JsonObject.Parse will throw an exception saying the string is not a valid JSON string.

bar.foo("{ \"bar\": 19 }");

For an object with a lot of fields, this could be time consuming and result in ugly code. Your better bet is to call JSON.stringify on the object to convert it to a string.

bar.foo(JSON.stringify({ bar: 19 }));

Or you could, of course, make some wrapper method that call JSON.stringify for you. Whatever works best for your situatiuon.

I'm starting up on Windows 8 also and struggled much of the morning with parsing JSON. This link got me to the point where I could deserialize my JSON string into a Windows.Data.Json.JsonObject.

Here's the code I wound up with:

        HttpResponseMessage response = await Client.SendAsync(RequestMessage);
        response.EnsureSuccessStatusCode();
        string json = response.Content.ReadAsString();
        ResponseObject = new JsonObject(json);

If you want to deserialize into your own data types, you could try System.Runtime.Serialization.Json.DataContractJsonSerializer. I didn't have a lot of luck with that; I kept getting back null from the ReadObject() method and didn't see a way to get any diagnostic information.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top