1

マルチレベルの JSON 配列からオブジェクトを取得しようとしています。これはテーブルの例です: array(2) {

  ["asd"]=>
  array(3) {
    ["id"]=>
    int(777)
    ["profile"]=>
    array(4) {
      ["username"]=>
      string(5) "grega"
      ["email"]=>
      string(18) "random@example.com"
      ["image"]=>
      string(16) "http...image.jpg"
      ["age"]=>
      int(26)
    }
    ["name"]=>
    string(5) "Grega"
  }
  ["be"]=>
  array(4) {
    ["username"]=>
    string(5) "grega"
    ["email"]=>
    string(18) "random@example.com"
    ["image"]=>
    string(16) "http...image.jpg"
    ["age"]=>
    int(26)
  }
}

私が到達しようとしている文字列は、電子メールのいずれかです (例)。これは私がそれを試す方法です:

public class getAsd
    {
        public string asd;
    }
    public class Profile
    {
        public string username { get; set; }
        public string email { get; set; }
        public string image { get; set; }
        public string age { get; set; }  
    }
}

そして、それを逆シリアル化するために使用JavaScriptSerilization.Deserilize<Asd>(jsonData);しますが、「プロファイル」で同じことを試みると、次のエラーが表示されます。

No parameterless constructor defined for type of 'System.String'.

JSON:

{"asd":{"id":777,"profile":{"username":"grega","email":"random@example.com","image":"http...image.jpg","age":26},"name":"Grega"},"be":{"username":"grega","email":"random@example.com","image":"http...image.jpg","age":26}}

そして、何が間違っているのでしょうか?

4

2 に答える 2

2

[編集: スマートは削除されました。OPは編集でJSONを追加しました]

JSON としてのプロファイル クラスは、次のようになります。

{
    "username":"grega",
    "email":"random@example.com",
    "image":"http...image.jpg",
    "age":"26",
    "roles": [
        {"name": "foo"},
        {"name": "bar"}
    ]
}

array"codearray"プロパティ名 ( ) またはプロパティ値 ( )の一部でない限り、JSON に表示されるべきではありません"There's no 'array' in JSON. There's no 'array' in JSON."

JSON 内のオブジェクトの配列は、角括弧[]で囲まれ、コンマで区切られています。JSON のプロファイルの配列/コレクション:

[
    {
        "username":"gretta",
        "email":"mrshansel@example.com",
        "image":"http...image.jpg",
        "age":"37",
        "roles": [
            {"name": "foo"},
            {"name": "bar"}
        ]
    },
    {
        "username":"methusaleh",
        "email":"old@men.org",
        "image":"http...image.jpg",
        "age":"2600",
        "roles": [
            {"name": "foo"},
            {"name": "},
            {"name": "bar"}
        ]
    },
    {
        "username":"goldilocks",
        "email":"porridge@bearshous.com",
        "image":"http...image.jpg",
        "age":"11",
        "roles": [
            {"name": "foo"}
        ]
    }
]

それはあなたの質問に完全には答えられないかもしれませんが、それから始めて質問を更新していただけますか?

編集:完全なアプローチについては、Hexxagonal によるこの回答を参照してください。

于 2012-05-03T19:31:36.723 に答える
1

さて、これがクラスの「基本」バージョンです。プロパティの最初の文字を大文字にするという標準に従う必要があります。あなたが以前にこれをしなかったので、私はそのスタイルを維持しました。

public class Type1
{
    public TypeAsd asd { get; set; }
    public TypeBe be { get; set; }
}

public class TypeAsd
{
    public int id { get; set; }
    public TypeBe profile { get; set; }
    public string name { get; set; }
}

public class TypeBe
{
    public string username { get; set; }
    public string email { get; set; }
    public string image { get; set; }
    public int age { get; set; }
}

逆シリアル化コードは次のようになります。

string jsonString = "{\"asd\":{\"id\":777,\"profile\":{\"username\":\"grega\",\"email\":\"random@example.com\",\"image\":\"http...image.jpg\",\"age\":26},\"name\":\"Grega\"},\"be\":{\"username\":\"grega\",\"email\":\"random@example.com\",\"image\":\"http...image.jpg\",\"age\":26}}";
JavaScriptSerializer serializer = new JavaScriptSerializer();
Type1 obj = serializer.Deserialize<Type1>(jsonString);
于 2012-05-03T19:41:41.457 に答える