1

I'm trying to get objects from a multi-level JSON array. This is the an example table: 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)
  }
}

The string I'm trying to reach is either of the emails (example). This is how I try it:

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; }  
    }
}

And then using JavaScriptSerilization.Deserilize<Asd>(jsonData); to deserilize it, but when I try the same with "Profile", it gives me the following error:

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}}

And idea what might be wrong?

4

2 回答 2

2

[EDIT: Smarm removed. OP did add JSON in an edit.]

Your profile class, as JSON, should resemble the following.

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

array should not show up in JSON unless its part of a property name ("codearray") or property value("There's no 'array' in JSON. There's no 'array' in JSON.").

Arrays of objects in JSON are encased in square brackets [] and comma-delimited. An array/collection of profiles in 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"}
        ]
    }
]

While that may not fully answer your question, could you start with that and update your question?

EDIT: See this answer by Hexxagonal for the complete approach.

于 2012-05-03T19:31:36.723 回答
1

Alright, here was what a "basic" version of your classes would be. You should really follow a standard of having properties have their first letter capitalized. Since you did not do this earlier, I maintained that style.

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; }
}

You deserialization code would then look something like the following:

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 回答