0

WPFアプリケーションでJson.netを使用しようとしています。接続しているWebサーバーから、次のような文字列を取得します。

[{"id": "11"、 "title": "デフォルト"、 "nclient": "3"}、{"id": "18"、 "title": "GrupoPorreiro"、 "nclient": "0 "}]

そしてそれを逆シリアル化するために使用しているコードはこれです。

public void preencheCampos()
    {
        try
        {

            HttpWebRequest request =
            (HttpWebRequest)WebRequest.Create("URL");
            //request.Method = "POST";
            request.ContentType = "application/json";
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11";
            request.CookieContainer = ApplicationState.GetValue<CookieContainer>("cookie");

            WebResponse response = request.GetResponse();
            Stream data = response.GetResponseStream();
            String html = String.Empty;
            request.CookieContainer = ApplicationState.GetValue<CookieContainer>("cookie");

            using (StreamReader sr = new StreamReader(data))
            {
                html = sr.ReadToEnd();
            }


            StringBuilder sb = new StringBuilder();
            List<string> entities = (List<string>)JsonConvert.DeserializeObject(html, typeof(List<string>));
            foreach (string items in entities)
            {
                sb.Append(items);
            }

         //...

        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.Message);
        }
    }

しかし、JsonConvert.DeserializeObjectの部分に到達すると、次のような例外が発生します。

「文字列の読み取りエラー。予期しないトークン:StartObject。パス'[0]'、1行目、2番目の位置。」

4

1 に答える 1

4

必要に応じて、クラスの簡単なコードをいくつか書きました。これは、投稿したJSON文字列で機能します。コメントでおっしゃったように、私はオブジェクトのクラスを作成し、それをリストアイテムとして使用しました。

static void Main(string[] args)
{
    string html = "[{\"id\":\"11\",\"title\":\"Default\",\"nclient\":\"3\"},{\"id\":\"18\",\"title\":\"GrupoPorreiro\",\"nclient\":\"0\"}]";

    List<item> entities = (List<item>)JsonConvert.DeserializeObject(html, typeof(List<item>));

}

class item
{
    public string id;
    public string title;
    public string nclient;
}
于 2013-01-23T15:34:42.923 に答える