Các json là loại kỳ lạ, nó giống như các sinh viên là tài sản của đối tượng "GetQuestion" , thật dễ dàng để trở thành một Danh sách .....
Giới thiệu về các thư viện bạn có thể sử dụng.
Và có thể có nhiều hơn nữa, nhưng đó là những gì tôi đã sử dụng
Giới thiệu về json I don' t bây giờ có thể giống như thế này
public class GetQuestions
{
public List<Student> Questions { get; set; }
}
public class Student
{
public string Code { get; set; }
public string Questions { get; set; }
}
void Main()
{
var gq = new GetQuestions
{
Questions = new List<Student>
{
new Student {Code = "s1", Questions = "Q1,Q2"},
new Student {Code = "s2", Questions = "Q1,Q2,Q3"},
new Student {Code = "s3", Questions = "Q1,Q2,Q4"},
new Student {Code = "s4", Questions = "Q1,Q2,Q5"},
}
};
//Using Newtonsoft.json. Dump is an extension method of [Linqpad][4]
JsonConvert.SerializeObject(gq).Dump();
}
Linqpad
và kết quả là thế này
{
"Questions":[
{"Code":"s1","Questions":"Q1,Q2"},
{"Code":"s2","Questions":"Q1,Q2,Q3"},
{"Code":"s3","Questions":"Q1,Q2,Q4"},
{"Code":"s4","Questions":"Q1,Q2,Q5"}
]
}
Có, tôi biết json là khác nhau, nhưng json mà bạn muốn với từ điển.
void Main()
{
var f = new Foo
{
GetQuestions = new Dictionary<string, string>
{
{"s1", "Q1,Q2"},
{"s2", "Q1,Q2,Q3"},
{"s3", "Q1,Q2,Q4"},
{"s4", "Q1,Q2,Q4,Q6"},
}
};
JsonConvert.SerializeObject(f).Dump();
}
class Foo
{
public Dictionary<string, string> GetQuestions { get; set; }
}
Và với từ điển là khi bạn muốn nó .....
{
"GetQuestions":
{
"s1":"Q1,Q2",
"s2":"Q1,Q2,Q3",
"s3":"Q1,Q2,Q4",
"s4":"Q1,Q2,Q4,Q6"
}
}
[bạn đã cố gắng gì] (http://whathaveyoutried.com)? Và danh sách của bạn trông như thế nào? –
[Json.NET] (http://json.net/) –