5

次のJSONデータを取得しています

[{"id":"1","text":"System Admin","target":{"jQuery1710835279177001846":12},"checked":true,"state":"open"},
{"id":"2","text":"HRMS","target":{"jQuery1710835279177001846":34},"checked":false,"state":"open"},
{"id":"3","text":"SDBMS","target":{"jQuery1710835279177001846":42},"checked":false},
{"id":"8","text":"Admin","target":{"jQuery1710835279177001846":43},"checked":false},
{"id":"9","text":"My test Admin","target":{"jQuery1710835279177001846":44},"checked":false,"state":"open"},
{"id":"24","text":"ModuleName","target":{"jQuery1710835279177001846":46},"checked":false,"state":"open"}]

強いタイプを使用してJson.Netを使用して解析しようとします

これは私のプロパティクラスです

public class testclass
    {
        public string id { get; set; }
        public string text { get; set; }
        public string @checked { get; set; }
        public string state { get; set; }
        public target jQuery1710835279177001846 { get; set; }

    }
    public class testclass2
    {
        public List<testclass> testclass1 { get; set; }

    }

    public class target
    {
        public string jQuery1710835279177001846 { get; set; }
    }

そしてここで私は例外を得ているデータにアクセスしようとしています

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'QuexstERP.Web.UI.Areas.SysAdmin.Controllers.testclass' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

私のコントローラーコードは次のようになります

 public void Test(string Name, object modeldata)
        {

            var obj = JsonConvert.DeserializeObject<testclass>(Name);

        }

C#でこの問題を解決する方法のアイデア

4

3 に答える 3

8

Json文字列には、が含まれているため、シリアル化された配列オブジェクトが含まれているように見えます[ ]。これは、配列オブジェクトのシリアル化後に形成されるJson文字列があることを意味します。したがって、配列オブジェクトに逆シリアル化する必要があるので、これを試してください

var obj = JsonConvert.DeserializeObject<List<testclass>>(jsonString);
于 2013-03-26T10:38:35.690 に答える
3

TestClassの配列があります。こうなるはずです。

var model= JsonConvert.DeserializeObject<List<testclass>>(Name);

なぜJSonConvertを使用しているのですか?MVC3では、このように行うことができます

return Json(yourmodel,JsonRequestBehavior.AllowGet);
于 2013-03-26T10:37:30.853 に答える
1

あなたのjsonオブジェクトはこのようなものです

{
      "id":"1",
      "text":"System Admin",
      "target":{
         "jQuery1710835279177001846":12
      },
      "checked":true,
      "state":"open"
}

こんな感じだと思います

{
      "id":"1",
      "text":"System Admin",
      "jQuery1710835279177001846":12,
      "checked":true,
      "state":"open"
}
于 2013-03-26T10:41:12.320 に答える