如何解析 JSON 文件

huangapple go评论100阅读模式
英文:

How to parse json file

问题

我在解析中遇到了问题

我的 JSON 文件:

  1. [{
  2. "username": "abc",
  3. "number": "1",
  4. "Coordinates": "3479087.7179635554,4723293.992024612,3587934.046241646,4936094.678770542"
  5. },
  6. {
  7. "username": "ab",
  8. "number": "2",
  9. "Coordinates": "3638076.736796722,4693942.173163104,3669874.540563355,4955662.558011548"
  10. }]

模型:

  1. namespace WebUygAPI.Models
  2. {
  3. public class DrawInfo
  4. {
  5. public string username { get; set; }
  6. public string number { get; set; }
  7. public string coordinates { get; set; }
  8. }
  9. }

我在控制器中遇到的问题是:

  1. [HttpGet]
  2. [Route("GetDraws")]
  3. public async Task<IActionResult> get()
  4. {
  5. string filePath = @"C:\Users\Casper\source\repos\WebUygAPI\WebUygAPI\LineData.json";
  6. using (StreamReader file = new StreamReader(filePath))
  7. {
  8. string o1 = file.ReadToEnd();
  9. }
  10. return Ok();
  11. }

当我进行调试时,我可以看到 o1 中的 JSON 文件,但我无法解析它。

我尝试进行解析,但出现了错误,例如

newtonsoft.json.jsonreaderexception: 在解析值时遇到意外字符

英文:

I have a problem with parsing

my json file:

  1. [{
  2. &quot;username&quot;: &quot;abc&quot;,
  3. &quot;number&quot;: &quot;1&quot;,
  4. &quot;Coordinates&quot;: &quot;3479087.7179635554,4723293.992024612,3587934.046241646,4936094.678770542&quot;
  5. },
  6. {
  7. &quot;username&quot;: &quot;ab&quot;,
  8. &quot;number&quot;: &quot;2&quot;,
  9. &quot;Coordinates&quot;: &quot;3638076.736796722,4693942.173163104,3669874.540563355,4955662.558011548&quot;
  10. }]

modal:

  1. namespace WebUygAPI.Models
  2. {
  3. public class DrawInfo
  4. {
  5. public string username { get; set; }
  6. public string number { get; set; }
  7. public string coordinates { get; set; }
  8. }
  9. }

The problem I'm having in the controller is:

  1. [HttpGet]
  2. [Route(&quot;GetDraws&quot;)]
  3. public async Task&lt;IActionResult&gt; get()
  4. {
  5. string filePath = @&quot;C:\Users\Casper\source\repos\WebUygAPI\WebUygAPI\LineData.json&quot;;
  6. using (StreamReader file = new StreamReader(filePath))
  7. {
  8. string o1 = file.ReadToEnd();
  9. }
  10. return Ok();
  11. }

When I'm debugging I can see json file in o1 but I couldn't parse it.

I tried to parse but I had errors such as

> newtonsoft.json.jsonreaderexception: unexpected character encountered while parsing value

答案1

得分: -1

我建议使用这段代码:

  1. 使用 Newtonsoft.Json;
  2. var json = File.ReadAllText(filePath);
  3. List<DrawInfo> info = JsonConvert.DeserializeObject<List<DrawInfo>>(json);
英文:

I recommend this code

  1. using Newtonsoft.Json;
  2. var json = File.ReadAllText(filePath);
  3. List&lt;DrawInfo&gt; info = JsonConvert.DeserializeObject&lt;List&lt;DrawInfo&gt;&gt;(json);
  4. </details>

huangapple
  • 本文由 发表于 2023年2月14日 21:50:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定