使用带有文件路径的 JSON 数组更新上次修改日期的 JSON 数组
Taking a JSON array with file paths to update the JSON array for date last modified
我目前有一个 API,我使用 dapper 连接到 SQL 数据库以查找与某个 ID 相关的文档的文件路径。结果以JSON格式返回如下:
[{"DOC-ID": 1, "DOCUMENT_FULL_PATH": "/PATH/FILENAME.DOC"},{"DOC-ID": 2, "DOCUMENT_FULL_PATH": "/PATH/FILENAME2.DOC"}]
我试图让我的 API 反序列化 JSON 数据然后 link 那也是一个模型(我不想使用模型,但我找到的唯一解决方案是使用JSON DOM 在 MS 网站上进行了简要讨论,但没有提供如何循环遍历 JSON 数组的示例,因此我无法继续使用此示例)。当我尝试反序列化小巧的查询结果时,我收到如下所示的错误(显示在代码的行项目中)。我不确定是什么触发了这个,因为我认为 QuerySingle 可以用这个方法反序列化。修复此错误后,我需要检查文件的上次修改日期并将该值保存到模型中,然后我再次需要序列化以发送到前端!我在这上面花了很多时间,所以非常感谢您的帮助!
[HttpPost]
public ActionResult MainReviewDocuments([FromForm] string ID)
{
//Using FormData on frontend
//checking ID exists on searching in dashboard
if (ID == null || ID.Length == 0)
{
return Ok(new { Result = "Failed" });
}
else
{
//We have parameters here just in case we want to use them
var UserID = HttpContext.User.FindFirst(ClaimTypes.Name).Value;
String query = "select dbo.A2Q_0132W_RR_IDDocumentation_JSON(@ID) as output";
using (var connection = new SqlConnection(connectionString))
{
var json = connection.QuerySingle<string>(query, new { ID = ID});
MainReviewDocuments? mainreviewdocuments = JsonSerializer.Deserialize<MainReviewDocuments>(json); // this line draws an error 'The JSON value could not be converted to Project.Models.MainReviewDocuments. Path: $ | LineNumber: 0 | BytePositionInLine: 1.'
var rootPath = Path.Combine(Directory.GetParent(env.ContentRootPath).ToString(), "Files");
foreach (var document in mainreviewdocuments)
{
filePath = Path.Combine(rootPath, document.DOCUMENT_FULL_PATH);
//Check file system for each file path and set last modified value to model object LAST_MODIFIED. Struggling with this as well
}
return Ok(mainreviewdocuments); // Can I use Ok() method to convert model back to JSON?
}
}
}
在您原来的通话中,您需要 de-serialize 到 List
:
MainReviewDocuments? mainreviewdocuments = JsonSerializer.Deserialize<List<MainReviewDocuments>>(json);
然后访问您的属性是必需的。
使用 Newtonsoft.Json
库:
您可以 de-serialize 您从 DB
收到的 JSON
字符串到以下 class:
public class MainReviewDocuments
{
[JsonProperty("DOC-ID")]
public int DOCID { get; set; }
public string DOCUMENT_FULL_PATH { get; set; }
}
或者您可以使用 dynamic
来 de-serialize 您的 JSON
:
var mainreviewdocuments = JsonSerializer.Deserialize<dynamic>(json);
然后访问属性,如下例所示。
您可以参考下面的工作example:
using System;
using Newtonsoft.Json;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var myJsonString=@"[{'DOC-ID': 1, 'DOCUMENT_FULL_PATH': '/PATH/FILENAME.DOC'},{'DOC-ID': 2, 'DOCUMENT_FULL_PATH': '/PATH/FILENAME2.DOC'}]";
var mainreviewdocuments =JsonConvert.DeserializeObject<List<MainReviewDocuments>>(myJsonString);
Console.WriteLine("Example using Model: \n");
foreach(var item in mainreviewdocuments)
{
Console.WriteLine(item.DOCID);
Console.WriteLine(item.DOCUMENT_FULL_PATH);
}
Console.WriteLine("\n");
Console.WriteLine("Example using Dynamic: \n");
//Example using dynamic
var mainreviewdocumentsDynamic=JsonConvert.DeserializeObject<dynamic>(myJsonString);
foreach(var item in mainreviewdocumentsDynamic)
{
Console.WriteLine(item["DOC-ID"]);
Console.WriteLine(item["DOCUMENT_FULL_PATH"]);
}
}
}
public class MainReviewDocuments
{
[JsonProperty("DOC-ID")]
public int DOCID { get; set; }
public string DOCUMENT_FULL_PATH { get; set; }
}
输出:
Example using Model:
1
/PATH/FILENAME.DOC
2
/PATH/FILENAME2.DOC
Example using Dynamic:
1
/PATH/FILENAME.DOC
2
/PATH/FILENAME2.DOC
使用 System.Text.Json
库:
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
public class Program
{
public static void Main()
{
var myJsonString="[{\"DOC-ID\": 1, \"DOCUMENT_FULL_PATH\": \"/PATH/FILENAME.DOC\"},{\"DOC-ID\": 2, \"DOCUMENT_FULL_PATH\": \"/PATH/FILENAME2.DOC\"}]";
var mainreviewdocuments = JsonSerializer.Deserialize<List<MainReviewDocuments>>(myJsonString);
Console.WriteLine("Example using Model: \n");
foreach(var item in mainreviewdocuments)
{
Console.WriteLine(item.DOCID);
Console.WriteLine(item.DOCUMENT_FULL_PATH);
}
Console.WriteLine("\n");
Console.WriteLine("Example using Dynamic: \n");
using (JsonDocument document = JsonDocument.Parse(myJsonString))
{
foreach (JsonElement element in document.RootElement.EnumerateArray())
{
Console.WriteLine(element.GetProperty("DOC-ID"));
Console.WriteLine(element.GetProperty("DOCUMENT_FULL_PATH"));
}
}
}
}
public class MainReviewDocuments
{
[JsonPropertyName("DOC-ID")]
public int DOCID { get; set; }
public string DOCUMENT_FULL_PATH { get; set; }
}
输出:
Example using Model:
1
/PATH/FILENAME.DOC
2
/PATH/FILENAME2.DOC
Example using Dynamic:
1
/PATH/FILENAME.DOC
2
/PATH/FILENAME2.DOC
工作示例:https://dotnetfiddle.net/nEjPIK
您可以在此 article
中阅读更多关于两个库之间比较的信息
我目前有一个 API,我使用 dapper 连接到 SQL 数据库以查找与某个 ID 相关的文档的文件路径。结果以JSON格式返回如下:
[{"DOC-ID": 1, "DOCUMENT_FULL_PATH": "/PATH/FILENAME.DOC"},{"DOC-ID": 2, "DOCUMENT_FULL_PATH": "/PATH/FILENAME2.DOC"}]
我试图让我的 API 反序列化 JSON 数据然后 link 那也是一个模型(我不想使用模型,但我找到的唯一解决方案是使用JSON DOM 在 MS 网站上进行了简要讨论,但没有提供如何循环遍历 JSON 数组的示例,因此我无法继续使用此示例)。当我尝试反序列化小巧的查询结果时,我收到如下所示的错误(显示在代码的行项目中)。我不确定是什么触发了这个,因为我认为 QuerySingle 可以用这个方法反序列化。修复此错误后,我需要检查文件的上次修改日期并将该值保存到模型中,然后我再次需要序列化以发送到前端!我在这上面花了很多时间,所以非常感谢您的帮助!
[HttpPost]
public ActionResult MainReviewDocuments([FromForm] string ID)
{
//Using FormData on frontend
//checking ID exists on searching in dashboard
if (ID == null || ID.Length == 0)
{
return Ok(new { Result = "Failed" });
}
else
{
//We have parameters here just in case we want to use them
var UserID = HttpContext.User.FindFirst(ClaimTypes.Name).Value;
String query = "select dbo.A2Q_0132W_RR_IDDocumentation_JSON(@ID) as output";
using (var connection = new SqlConnection(connectionString))
{
var json = connection.QuerySingle<string>(query, new { ID = ID});
MainReviewDocuments? mainreviewdocuments = JsonSerializer.Deserialize<MainReviewDocuments>(json); // this line draws an error 'The JSON value could not be converted to Project.Models.MainReviewDocuments. Path: $ | LineNumber: 0 | BytePositionInLine: 1.'
var rootPath = Path.Combine(Directory.GetParent(env.ContentRootPath).ToString(), "Files");
foreach (var document in mainreviewdocuments)
{
filePath = Path.Combine(rootPath, document.DOCUMENT_FULL_PATH);
//Check file system for each file path and set last modified value to model object LAST_MODIFIED. Struggling with this as well
}
return Ok(mainreviewdocuments); // Can I use Ok() method to convert model back to JSON?
}
}
}
在您原来的通话中,您需要 de-serialize 到 List
:
MainReviewDocuments? mainreviewdocuments = JsonSerializer.Deserialize<List<MainReviewDocuments>>(json);
然后访问您的属性是必需的。
使用 Newtonsoft.Json
库:
您可以 de-serialize 您从 DB
收到的 JSON
字符串到以下 class:
public class MainReviewDocuments
{
[JsonProperty("DOC-ID")]
public int DOCID { get; set; }
public string DOCUMENT_FULL_PATH { get; set; }
}
或者您可以使用 dynamic
来 de-serialize 您的 JSON
:
var mainreviewdocuments = JsonSerializer.Deserialize<dynamic>(json);
然后访问属性,如下例所示。
您可以参考下面的工作example:
using System;
using Newtonsoft.Json;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var myJsonString=@"[{'DOC-ID': 1, 'DOCUMENT_FULL_PATH': '/PATH/FILENAME.DOC'},{'DOC-ID': 2, 'DOCUMENT_FULL_PATH': '/PATH/FILENAME2.DOC'}]";
var mainreviewdocuments =JsonConvert.DeserializeObject<List<MainReviewDocuments>>(myJsonString);
Console.WriteLine("Example using Model: \n");
foreach(var item in mainreviewdocuments)
{
Console.WriteLine(item.DOCID);
Console.WriteLine(item.DOCUMENT_FULL_PATH);
}
Console.WriteLine("\n");
Console.WriteLine("Example using Dynamic: \n");
//Example using dynamic
var mainreviewdocumentsDynamic=JsonConvert.DeserializeObject<dynamic>(myJsonString);
foreach(var item in mainreviewdocumentsDynamic)
{
Console.WriteLine(item["DOC-ID"]);
Console.WriteLine(item["DOCUMENT_FULL_PATH"]);
}
}
}
public class MainReviewDocuments
{
[JsonProperty("DOC-ID")]
public int DOCID { get; set; }
public string DOCUMENT_FULL_PATH { get; set; }
}
输出:
Example using Model:
1
/PATH/FILENAME.DOC
2
/PATH/FILENAME2.DOC
Example using Dynamic:
1
/PATH/FILENAME.DOC
2
/PATH/FILENAME2.DOC
使用 System.Text.Json
库:
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
public class Program
{
public static void Main()
{
var myJsonString="[{\"DOC-ID\": 1, \"DOCUMENT_FULL_PATH\": \"/PATH/FILENAME.DOC\"},{\"DOC-ID\": 2, \"DOCUMENT_FULL_PATH\": \"/PATH/FILENAME2.DOC\"}]";
var mainreviewdocuments = JsonSerializer.Deserialize<List<MainReviewDocuments>>(myJsonString);
Console.WriteLine("Example using Model: \n");
foreach(var item in mainreviewdocuments)
{
Console.WriteLine(item.DOCID);
Console.WriteLine(item.DOCUMENT_FULL_PATH);
}
Console.WriteLine("\n");
Console.WriteLine("Example using Dynamic: \n");
using (JsonDocument document = JsonDocument.Parse(myJsonString))
{
foreach (JsonElement element in document.RootElement.EnumerateArray())
{
Console.WriteLine(element.GetProperty("DOC-ID"));
Console.WriteLine(element.GetProperty("DOCUMENT_FULL_PATH"));
}
}
}
}
public class MainReviewDocuments
{
[JsonPropertyName("DOC-ID")]
public int DOCID { get; set; }
public string DOCUMENT_FULL_PATH { get; set; }
}
输出:
Example using Model:
1
/PATH/FILENAME.DOC
2
/PATH/FILENAME2.DOC
Example using Dynamic:
1
/PATH/FILENAME.DOC
2
/PATH/FILENAME2.DOC
工作示例:https://dotnetfiddle.net/nEjPIK
您可以在此 article
中阅读更多关于两个库之间比较的信息