Hello everyone.

The Mixed Reality Forums here are no longer being used or maintained.

There are a few other places we would like to direct you to for support, both from Microsoft and from the community.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.

If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.

And always feel free to hit us up on Twitter @MxdRealityDev.

How to use Windows.Data.Json

Hi, I am new to Unity and C# and hope someone can help.

I need to post an image and some types to a server so I decided to make a Json http post request. But I am not sure how to use the Windows.Data.Json package. Could anyone show me some code how to package an image, and some value name values pairs to Json using Windows.Data.Json?

Thank you very much.

Answers

  • Here is a quick and dirty sample I used to pass Json over to an internally hosted WebAPI from the HoloLens:

    You need to add the following using statements to the top of your class with the WINDOWS_UWP declaration to prevent compilation errors in unity (ref: http://docs.unity3d.com/Manual/windowsstore-scripts.html).

    #if WINDOWS_UWP
    using Windows.Web.Http;
    using Windows.Web.Http.Headers;
    #endif
    

    Once you have that you can then use the following example to pass Json content to your Api

        #if WINDOWS_UWP
                HttpClient client = new HttpClient();
    
                HttpStringContent content = null;
                HttpRequestMessage request = null;
                HttpResponseMessage response = null;
                Uri uri = new Uri("<path to api>");
    
                Windows.Data.Json.JsonObject json = new Windows.Data.Json.JsonObject();
                json["Name"] = JsonValue.CreateStringValue("x");
                json["Value"] = JsonValue.CreateStringValue("x value");
    
                content = new HttpStringContent(json.ToString(), Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");
    
                request = new HttpRequestMessage(HttpMethod.Post, uri);
                request.Content = content;
    
                client.DefaultRequestHeaders.Accept.Add(new HttpMediaTypeWithQualityHeaderValue("application/json"));
    
                response = client.SendRequestAsync(request, HttpCompletionOption.ResponseContentRead).AsTask().Result;
    
                }
          #endif
    

    I hope this helps.

  • @Malcolm Thank you very much. It helps a lot.

  • @fireman for the image, if you want to pass it over as JSON you could convert the image bytes to a Base64 encoded

    Convert.ToBase64String(myImgBytes);

    JSON string (using sample code to what @Malcolm provided) and decode them on the server.

    Windows Holographic User Group Redmond

    WinHUGR.org - - - - - - - - - - - - - - - - - - @WinHUGR
    WinHUGR YouTube Channel -- live streamed meetings

  • @Malcolm said:
    Here is a quick and dirty sample I used to pass Json over to an internally hosted WebAPI from the HoloLens:

    You need to add the following using statements to the top of your class with the WINDOWS_UWP declaration to prevent compilation errors in unity (ref: http://docs.unity3d.com/Manual/windowsstore-scripts.html).

    #if WINDOWS_UWP
    using Windows.Web.Http;
    using Windows.Web.Http.Headers;
    #endif
    

    Once you have that you can then use the following example to pass Json content to your Api

        #if WINDOWS_UWP
                HttpClient client = new HttpClient();
                
                HttpStringContent content = null;
                HttpRequestMessage request = null;
                HttpResponseMessage response = null;
                Uri uri = new Uri("<path to api>");
        
                Windows.Data.Json.JsonObject json = new Windows.Data.Json.JsonObject();
                json["Name"] = JsonValue.CreateStringValue("x");
                json["Value"] = JsonValue.CreateStringValue("x value");
         
                content = new HttpStringContent(json.ToString(), Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");
        
                request = new HttpRequestMessage(HttpMethod.Post, uri);
                request.Content = content;
                
                client.DefaultRequestHeaders.Accept.Add(new HttpMediaTypeWithQualityHeaderValue("application/json"));
        
                response = client.SendRequestAsync(request, HttpCompletionOption.ResponseContentRead).AsTask().Result;
        
                }
          #endif
    

    I hope this helps.

    Is there a way to write this in c++ ?!

Sign In or Register to comment.