我知道如何在表中存储一条简单的JSON消息,但是如果我生成一个JSON数组,如何在这个表中轻松存储我的数据? 例如:
[{" ID":0,"数据1" 123"数据2":"字符串1""时间戳& #34;:" /日期(-62135596800000-0000)/"},{" ID":0,"数据1":456,"数据2":"字符串2""时间戳":" /日期(-62135596800000-0000)/"}]
我试过这样的事情,但它没有工作它没有编译:
public class Table1
{
public List<Table1> Table1LIST{ get; set; }
[AutoIncrement]
public int ID { get; set; }
public int Data1{ get; set; }
public string Data2{ get; set; }
public DateTime Timestamp { get; set; }
}
// Add Table1 via POST
[Route("/Table1ADD", Verbs = "POST")]
public class Table1 ADD
{
public int ID { get; set; }
public int Data1{ get; set; }
public string Data2{ get; set; }
public DateTime Timestamp { get; set; }
}
// Add multi Table1 via POST
[Route("/Table1ADDMulti", Verbs = "POST")]
public class Table1 ADDMulti
{
public List<Table1 > Table1LIST { get; set; }
}
// Store JSON array
public object Post(Table1Multi request)
{
var data =new Table1()
{
Table1LIST = request.Table1LIST
};
CRUDFunctions CRUD = new CRUDFunctions(Db);
return CRUD.AddData(data);
}
我使用函数insert来存储我的数据(CRUD.AddData())。此函数指定可以同时存储多个行。
这个想法是避免发送多条消息。 提前致谢
答案 0 :(得分:2)
您非常接近,除非您不应向List<Table1>
添加Table1
。而是创建Table1
,如下所示,并创建一个简单的DTO,如Table1Multiple
来处理多个值。
[Route("/Table1","POST")]
public class Table1 : IReturn<int>
{
[AutoIncrement]
public int ID { get; set; }
public int Data1 { get; set; }
public string Data2 { get; set; }
public DateTime Timestamp { get; set; }
}
[Route("/Table1/Multi","POST")]
public class Table1Multiple : IReturn<List<int>>
{
public List<Table1> Values { get; set; }
}
在服务中,您应该重复使用用于存储单个记录的代码,并简单地迭代Table1
Values
列表。
class TestService : Service
{
// Stores a single Table1 record
public int Post(Table1 request)
{
// Add your method to store a single record
var CRUD = new CRUDFunctions(Db);
return CRUD.AddData(request); // Return the ID of the created record
}
// Stores multiple Table1 records
public List<int> Post(Table1Multiple request)
{
// Return a list of the record Ids
var result = new List<int>();
// Process each request, through the single record code
foreach(var value in request.Values)
result.Add(Post(value));
return result;
}
}
I don't get anything in "request" in debug mode.
你有两个选择。您可以更改请求以使用属性values
{values:[{"ID":0,"Data1":123,"Data2":"String1","Timestamp":"/Date(-62135596800000-0000)/"},
{"ID":0,"Data1":456,"Data2":"String2","Timestamp":"/Date(-62135596800000-0000)/"}]}
或您可以将请求DTO更改为从List<Table1>
扩展而来:
//
[Route("/Table1/Multi","POST")]
public class Table1Multiple : List<Table1>, IReturn<List<int>>
{
}
然后修改服务,使其显示var value in request
而不是var value in request.Values
class TestService : Service
{
// Stores a single Table1 record
public int Post(Table1 request)
{
// Add your method to store a single record
var CRUD = new CRUDFunctions(Db);
return CRUD.AddData(request); // Return the ID of the created record
}
// Stores multiple Table1 records
public List<int> Post(Table1Multiple request)
{
// Return a list of the record Ids
var result = new List<int>();
// Process each request, through the single record code
foreach(var value in request) // Collection changed here
result.Add(Post(value));
return result;
}
}