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) "[email protected]"
      ["image"]=>
      string(16) "http...image.jpg"
      ["age"]=>
      int(26)
    }
    ["name"]=>
    string(5) "Grega"
  }
  ["be"]=>
  array(4) {
    ["username"]=>
    string(5) "grega"
    ["email"]=>
    string(18) "[email protected]"
    ["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":"[email protected]","image":"http...image.jpg","age":26},"name":"Grega"},"be":{"username":"grega","email":"[email protected]","image":"http...image.jpg","age":26}}

And idea what might be wrong?

3
  • Can you post the actual JSON? Commented May 3, 2012 at 19:08
  • Yeah, same as @Kroehre - the above isnt JSON. Commented May 3, 2012 at 19:12
  • The JSON doesn't even closesly match your objects. Let's see what we can do. Commented May 3, 2012 at 19:18

2 Answers 2

2

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

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

{
    "username":"grega",
    "email":"[email protected]",
    "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":"[email protected]",
        "image":"http...image.jpg",
        "age":"37",
        "roles": [
            {"name": "foo"},
            {"name": "bar"}
        ]
    },
    {
        "username":"methusaleh",
        "email":"[email protected]",
        "image":"http...image.jpg",
        "age":"2600",
        "roles": [
            {"name": "foo"},
            {"name": "},
            {"name": "bar"}
        ]
    },
    {
        "username":"goldilocks",
        "email":"[email protected]",
        "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.

Sign up to request clarification or add additional context in comments.

2 Comments

He's got his JSON way at the end. It's easy to miss and I think he edited the question to stick it in.
@Hexxagonal So he does...thanks. I think I'll leave the JSON explanation up for now (until/unless someone requests I pull it), but I'm upvoting your answer.
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\":\"[email protected]\",\"image\":\"http...image.jpg\",\"age\":26},\"name\":\"Grega\"},\"be\":{\"username\":\"grega\",\"email\":\"[email protected]\",\"image\":\"http...image.jpg\",\"age\":26}}";
JavaScriptSerializer serializer = new JavaScriptSerializer();
Type1 obj = serializer.Deserialize<Type1>(jsonString);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.