C# 解析多级JSON数组

C# 解析多级JSON数组,c#,.net,json,C#,.net,Json,我试图从多级JSON数组中获取对象。这是一个示例表: 阵列(2){ 我尝试访问的字符串是其中一封电子邮件(示例)。我是这样尝试的: public class getAsd { public string asd; } public class Profile { public string username { get; set; } public string email { get; set; }

我试图从多级JSON数组中获取对象。这是一个示例表: 阵列(2){

我尝试访问的字符串是其中一封电子邮件(示例)。我是这样尝试的:

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; }  
    }
}
然后使用
JavaScriptSerialization.Deserilize(jsonData);
对其进行反序列化,但当我尝试使用“Profile”进行同样的操作时,会出现以下错误:

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


你知道怎么回事吗?

[编辑:Smarm已删除。OP在编辑中添加了JSON。]

您的profile类(作为JSON)应该类似于以下内容

{
    "username":"grega",
    "email":"random@example.com",
    "image":"http...image.jpg",
    "age":"26",
    "roles": [
        {"name": "foo"},
        {"name": "bar"}
    ]
}
array
不应显示在JSON中,除非它是属性名(
“coderaray”
)或属性值(
“JSON中没有“array”。JSON中没有“array”。
)的一部分

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"}
        ]
    }
]
虽然这可能无法完全回答您的问题,但您能否从这一点开始并更新您的问题

编辑: 请参阅以了解完整的方法。

好的,这里是类的“基本”版本。您应该真正遵循将属性的第一个字母大写的标准。因为您之前没有这样做,所以我保留了这种样式

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);
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=新的JavaScriptSerializer();
Type1 obj=序列化程序。反序列化(jsonString);

你能发布实际的JSON吗?是的,与@Kroehre相同-上面的不是JSON。JSON甚至与你的对象不匹配。让我们看看我们能做些什么。他在最后有自己的JSON方式。很容易被忽略,我想他编辑了这个问题以坚持它。@Hexxagonal,他是这样做的……谢谢。我想我现在就把JSON的解释留在这里(直到/除非有人要求我拉它),但我投票支持你的答案。
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);