1

こんにちは私はWebAPIとこのAPIと通信するwindows8メトロアプリを作成しています。

簡単な例を使用します

API

public IEnumerable<string> Get() {
        return new string[] { "value1", "value2" };
}

応用

using (HttpClient client = new HttpClient())
        {
            using (var response = await client.GetAsync(this.apiUri.GetUri("content", "Get")))
            {
                var result = await response.Content.ReadAsStringAsync();

            }
        }

これで、変数の結果はAPIからの応答になります。しかし、これは形式の文字列です

"[\"value1\",\"value2\"]"

どうすればこれを解析/変換できますか?

4

3 に答える 3

2

WebAPIはデフォルトでJSONを返します。XMLの送信をリクエストするか、JSONパーサーを使用できます。

于 2012-08-08T16:03:58.663 に答える
1

あなたの場合、Json.Netを使用して逆シリアル化するのは簡単です。

var json = "[\"value1\",\"value2\"]";
var values = JsonConvert.DeserializeObject<string[]>(json);
于 2012-08-08T17:41:23.700 に答える
0

I use my class (MVCClient.cs) in silverlight to communicate with MVC WebAPI. In your case you'd just want to call :

DoGetMessage<string>(yourUrl, new DataDelegateArray<string>(yourCallbackFunction));

The lines that you are missing in your code would be :

DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
T data = (T)serializer.ReadObject(stream);

Where T is string[].

MVCClient.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;
public class MVCClient
    {
        public delegate void DataDelegateArray<T>(T[] objects);
        public delegate void DataDelegate<U>(U obj);

    #region Communication Functions
        public void DoPostMessage<T>(string queryURL, T item, ProcessPOSTResult<T> callback)
        {
            DoPostMessage<T, T>(queryURL, item, callback);
        }

        public void DoPostMessage<TPost, TReply>(string queryURL, TPost item, ProcessPOSTResult<TReply> callback)
        {
#if SILVERLIGHT
            Uri uri = new Uri(queryURL, UriKind.Absolute);

            var request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "POST";
            request.ContentType = "application/json";
            request.BeginGetRequestStream(result =>
                {
                    var req = (HttpWebRequest)result.AsyncState;
                    var stream = req.EndGetRequestStream(result);

                    if (stream != null)
                    {
                        var data = new
                        {
                            name = item
                        };


                        System.IO.MemoryStream ms = new System.IO.MemoryStream();
                        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(TPost));
                        serializer.WriteObject(stream, item);
                        stream.Close();
                    }

                    req.BeginGetResponse((requestResult) =>
                    {
                        var req2 = (HttpWebRequest)requestResult.AsyncState;
                        var response = req2.EndGetResponse(requestResult);
                        var responseStream = response.GetResponseStream();
                        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(TReply));
                        TReply responseItem = (TReply)serializer.ReadObject(responseStream);

                        System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {
                            callback(responseItem);
                        });

                    }, req);
                }, request);

#endif
        }

        public void DoGetMessage<T>(string queryURL, DataDelegateArray<T> callback)
        {
            DoGetMessage<T[]>(queryURL, result => callback(result));
        }

        public void DoGetMessage<T>(string queryURL, DataDelegate<T> callback)
        {
#if SILVERLIGHT
            Uri uri = new Uri(queryURL, UriKind.Absolute);

            var request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "GET";
            request.Accept = "text/json";
            request.BeginGetResponse((result) =>
            {
                var response = request.EndGetResponse(result);
                var stream = response.GetResponseStream();

                if (stream != null)
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
                    T data = (T)serializer.ReadObject(stream);
                    System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        callback(data);
                    });
                }
                else
                    System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        callback(default(T));
                    });
            }, null);
#endif
        }
        #endregion
    }
于 2012-08-08T16:19:39.710 に答える