我在存储中有一堆Json文件,可以循环浏览以选择符合我的条件的设置列表。即使Json文件具有不同的数据和结构,它们也都具有1个共同点-具有以下结构的Metadata属性:
"meta": {
"source": "user1",
"createdDate": "2018-07-16T16:36:58.6471066+01:00",
"recordCount": 12
},
我想提取每个文件的元数据并将结果存储在具有以下结构的新DTO中:
"data": [
{
"source": "User1",
"createdDate": "2018-07-16T16:36:58.6471066+01:00",
"recordCount":12
},
{
"source": "User2",
"createdDate": "2018-07-15T13:01:23.5611259+01:00",
"recordCount":18
}
]
此刻我的代码如下:
foreach (string s in objectType) //objectType is a simple string array for my file criteria
{
var fileData = storage.GetS3(s, testOrg); //custom method to get file from storage, returns the json file contents using StreamReader. Returns null if no file found
if (fileData != null)
{
var response = JsonConvert.DeserializeObject(fileData);
//extract metadata from file to populate DTO
}
}
感谢您的帮助。一般来说,我是C#的新手,所以请放轻松!
我的DTO当前如下所示,但可以更改:
public class FileStatusResponse
{
[JsonProperty("data")]
public FileStatusDetail[] Data { get; set; }
}
public class FileStatusDetail
{
[JsonProperty("source")]
public string FileType { get; set; }
[JsonProperty("recordCount")]
public string RecordCount { get; set; }
[JsonProperty("refreshDate")]
public DateTime RefreshDate { get; set; }
}
答案 0 :(得分:0)
好吧,我似乎已经用它排序了:
var fileStatusList = new List<FileStatusDetail>();
var storage = new S3Manager();
foreach (string s in objectType)
{
var fileData = storage.GetS3(s, organisationCode);
if (fileData != null)
{
var parsedObject = JObject.Parse(fileData);
var parsedJson = parsedObject["meta"].ToString();
var json = JsonConvert.DeserializeObject<Metadata>(parsedJson);
fileStatusList.Add(new FileStatusDetail
{
source = s,
RecordCount = json.RecordCount,
RefreshDate = json.CreatedDate
});
}
}
感谢itsme86为我指出正确的方向