我有一个需要保存为sql server表的json文件。这是test.json,其中包含有关学生作业的详细信息。
export class UserInfoService {
constructor(private employeeService: EmployeeService) {
this.handleSuccess = this.handleSuccess.bind(this);
this.processData = this.processData.bind(this);
}
getEmployeeInfo(event?: Refresher) {
// console.log(event);
return this.employeeService
.getEmployeeInfo()
.map(this.handleSuccess)
.finally(this.handleFinally);
}
handleFinally = (event?: Refresher) => {
console.log(' handle finally ', event);
if (event != null) {
console.log(' event ', event);
event.complete();
}
}
}
下面是C#程序。 我不知道如何访问“ smalldesc”和“ collegeCode”。
[{
"Studentid": "001006360",
"Grade": "2",
"ExtraWork": {
"TopRecommended": ["000133692",
"102067155",
"887273865"],
"OtherCourses": ["228963647",
"138909237",
"899791144",
"216165613",
"113239563"]
},
"Courses": [{
"smalldesc": "this is a test ",
"Details": {
"description": "Summary of the course",
"collegeCode": "32466"
}
},
{
"smalldesc": "Second test",
"Details": {
"description": "Business- Course Summary",
"collegeCode": "32469"
}
}]
}]
Json对象类定义:
var jsonText = File.ReadAllText(@"C:\test.json");
var ser = JsonConvert.DeserializeObject<List<RootObject>>(jsonText);
for (int i = 0; i < ser.Count; i++)
{
string Studentid = ser[i].Studentid;
string Grade = ser[i].Grade;
var result = JsonConvert.DeserializeObject<List<Course>>(jsonText);
for (int k = 0; k < result.Count; k++)
{
string smalldesc = result[k].smalldesc;
string collegeCode = result[k].Details.collegeCode;
}
}
保存到sql服务器表的最佳方法是什么。
答案 0 :(得分:2)
此行:
@Component({
selector: 'app-tasklist-items-grid',
templateUrl: './tasklist-items-grid.component.html',
styleUrls: ['./tasklist-items-grid.component.scss'],
providers: [TasklistItemsService, BsModalService]
})
export class TasklistItemsGridComponent implements OnInit {
taskListItem: TaskListItemViewModel;
constructor() {
// here or in ngOnInit
// this.taskListItem = new TaskListItemViewModel();
}
// .....
ngOnInit() {
// this.taskListItem = new TaskListItemViewModel();
}
convertToViewModel = (item: ITaskListItemViewModel) => item;
}
// in another file or same component you can write this class
export class TaskListItemViewModel implements ITaskListItemViewModel{
id?: number;
tasklistId: number;
typeOfTask: number;
statusId: number;
created_at: Date;
updated_at: Date;
checked: Boolean;
rowVersion: number;
}
interface ITaskListItemViewModel{
id: number;
tasklistId: number;
typeOfTask: number;
statusId: number;
created_at: Date;
updated_at: Date;
checked: Boolean;
rowVersion: number;
}
已经已经为您完成了所有反序列化,因此无需在循环内再次调用它。
您的代码可以像这样简单:
var ser = JsonConvert.DeserializeObject<List<RootObject>>(jsonText);
仅供参考:在迭代集合时,使用var ser = JsonConvert.DeserializeObject<List<RootObject>>(jsonText);
foreach (var s in ser)
{
string Studentid = s.Studentid;
string Grade = s.Grade;
foreach(var course in ser.Courses)
{
string smalldesc = course .smalldesc;
string details = course .Details.collegeCode;
}
}
循环要容易得多(假设您的集合类型实现了foreach
,其中大多数包含的集合都将实现)。