我的 json 反序列化遇到一些问题。我必须创建一个 REST API 来在转换后的 json 文件中搜索特定结果。首先,它必须检查类型是否为
001
,然后必须列出以用户输入字符串开头的 roadname
的所有结果。
Json 文件示例
{"type":"001","kommuneCode":"0621","roadCode":"8713","timestamp":"199109231200","roadToNextKommuneCode":"0000","roadToNextRoadCode":"0000","roadToPreviousKommuneCode":"0000","roadToPreviousRoadCode":"0000","startDate":"190001010000","roadName":"Topasvej ","ExpandedRoadName":"Topasvej "}
{"type":"003","kommuneCode":"0621","roadCode":"8713","houseNumberFrom":"002 ","HouseNumberTo":"030 ","evenUneven":"L","timestamp":"201809292139","townName":"Seest "}
{"type":"003","kommuneCode":"0621","roadCode":"8713","houseNumberFrom":"001 ","HouseNumberTo":"027 ","evenUneven":"U","timestamp":"201809292139","townName":"Seest "}
{"type":"004","kommuneCode":"0621","roadCode":"8713","houseNumberFrom":"002 ","HouseNumberTo":"030 ","evenUneven":"L","timestamp":"201809292110","postNumber":"6000","postalDistrict":"Kolding "}
{"type":"004","kommuneCode":"0621","roadCode":"8713","houseNumberFrom":"001 ","HouseNumberTo":"027 ","evenUneven":"U","timestamp":"201809292110","postNumber":"6000","postalDistrict":"Kolding "}
{"type":"009","kommuneCode":"0621","roadCode":"8713","houseNumberFrom":"002 ","houseNumberTo":"030 ","evenUneven":"L","timestamp":"201809292202","churchCode":"23","districtText":"Seest "}
{"type":"009","kommuneCode":"0621","roadCode":"8713","houseNumberFrom":"001 ","houseNumberTo":"027 ","evenUneven":"U","timestamp":"201809292202","churchCode":"23","districtText":"Seest "}
{"type":"013","kommuneCode":"0621","roadCode":"8713","houseNumberFrom":"002 ","houseNumberTo":"030 ","evenUneven":"L","timestamp":"201809292203","authorityCode":"8940","districtText":"Seest,Kolding "}
{"type":"013","kommuneCode":"0621","roadCode":"8713","houseNumberFrom":"001 ","houseNumberTo":"027 ","evenUneven":"U","timestamp":"201809292203","authorityCode":"8940","districtText":"Seest,Kolding "}
{"type":"014","kommuneCode":"0621","roadCode":"8713","houseNumberFrom":"002 ","houseNumberTo":"030 ","evenUneven":"L","timestamp":"201809292202","electionCode":"10","districtText":"Seest "}
{"type":"014","kommuneCode":"0621","roadCode":"8713","houseNumberFrom":"001 ","houseNumberTo":"027 ","evenUneven":"U","timestamp":"201809292202","electionCode":"10","districtText":"Seest "}
{"type":"001","kommuneCode":"0621","roadCode":"8724","timestamp":"199109231200","roadToNextKommuneCode":"0000","roadToNextRoadCode":"0000","roadToPreviousKommuneCode":"0000","roadToPreviousRoadCode":"0000","startDate":"190001010000","roadName":"Toppen ","ExpandedRoadName":"Toppen "}
我的模型类
SearchAllRoadNames
我在我的rest api中使用
namespace John_Høeg_opgave_4._1.Models
{
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection.Metadata;
public class SearchAllRoadNames
{
public string type { get; set; }
public string kommuneCode { get; set; }
public string roadCode { get; set; }
public string timestamp { get; set; }
public string roadToNextKommuneCode { get; set; }
public string roadToNextRoadCode { get; set; }
public string roadToPreviousKommuneCode { get; set; }
public string roadToPreviousRoadCode { get; set; }
public string startDate { get; set; }
public string roadName { get; set; }
public string ExpandedRoadName { get; set; }
}
}
还有我的其余 api RoadNamesController
using John_Høeg_opgave_4._1.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Text.Json.Nodes;
namespace John_Høeg_opgave_4._1.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class RoadNamesController : ControllerBase
{
[HttpGet]
public SearchAllRoadNames Get(string value)
{
SearchAllRoadNames roadNames = new SearchAllRoadNames();
var jsonFilePath = "C:\\Users\\Saphy\\OneDrive\\Softwareudvikling\\SystemIntegration\\John Høeg opgave 4.1\\path.json";
var jsonString = System.IO.File.ReadAllText(jsonFilePath);
List<SearchAllRoadNames> jsonList = JsonConvert.DeserializeObject<List<SearchAllRoadNames>>(jsonString);
// Filter the list based on the specified type and road name filter
string searchType = "001";
string roadNameFilter = value;
List<SearchAllRoadNames> filteredList = jsonList.Where(x => x.type == searchType && x.roadName.StartsWith(roadNameFilter)).ToList();
// Display the filtered results
foreach (SearchAllRoadNames obj in filteredList)
{
roadNames.roadName = obj.roadName;
}
return roadNames;
}
}
}
我得到的错误是:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[John_Høeg_opgave_4._1.Models.SearchAllRoadNames]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'type', line 1, position 8.'
您的 JSON 不是数组,但您正尝试将其反序列化为一个数组。
将整个内容放在方括号中,在每行末尾添加逗号,这样就可以了。
此外,微软还更新了他们的 json 库来取代 Newtonsoft。使用 System.Text.Json 来获取与 Newtonsoft 几乎相同的调用。
设法找到解决我的问题的方法。感谢 gbjbaanb 找到了最初的问题。
修复了 JSON。这样它的格式正确,这样反序列化器就可以正确读取它。
示例代码
[
{
"type": "001",
"kommuneCode": "0010",
"roadCode": "0070",
"timestamp": "199109231200",
"roadToNextKommuneCode": "0000",
"roadToNextRoadCode": "0000",
"roadToPreviousKommuneCode": "0000",
"roadToPreviousRoadCode": "0000",
"startDate": "190001010000",
"roadName": "Norge ",
"ExpandedRoadName": "Norge "
},
{
"type": "001",
"kommuneCode": "0010",
"roadCode": "0071",
"timestamp": "199109231200",
"roadToNextKommuneCode": "0000",
"roadToNextRoadCode": "0000",
"roadToPreviousKommuneCode": "0000",
"roadToPreviousRoadCode": "0000",
"startDate": "190001010000",
"roadName": "Sverige ",
"ExpandedRoadName": "Sverige "
},
{
"type": "001",
"kommuneCode": "0010",
"roadCode": "0072",
"timestamp": "199109231200",
"roadToNextKommuneCode": "0000",
"roadToNextRoadCode": "0000",
"roadToPreviousKommuneCode": "0000",
"roadToPreviousRoadCode": "0000",
"startDate": "190001010000",
"roadName": "Finland ",
"ExpandedRoadName": "Finland "
},
我还注意到我的模型视图中存在问题。它缺少它的构造函数......新手错误:P
namespace John_Høeg_opgave_4._1.Models
{
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection.Metadata;
public class SearchAllRoadNames
{
public SearchAllRoadNames()
{
}
public string type { get; set; }
public string kommuneCode { get; set; }
public string roadCode { get; set; }
public string timestamp { get; set; }
public string roadToNextKommuneCode { get; set; }
public string roadToNextRoadCode { get; set; }
public string roadToPreviousKommuneCode { get; set; }
public string roadToPreviousRoadCode { get; set; }
public string startDate { get; set; }
public string roadName { get; set; }
public string ExpandedRoadName { get; set; }
}
}
最后,我完全更改了其余 API 的代码,以便它正常工作。不小心做的,所以只显示 1 个结果。它显示的是整个班级,而不是道路名称。
using John_Høeg_opgave_4._1.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Text.Json.Nodes;
namespace John_Høeg_opgave_4._1.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class RoadNamesController : ControllerBase
{
[HttpGet]
public List<string> Get(string value)
{
var jsonFilePath = "C:\\Users\\Saphy\\OneDrive\\Softwareudvikling\\SystemIntegration\\John Høeg opgave 4.1\\class001File.json";
var jsonString = System.IO.File.ReadAllText(jsonFilePath);
var jsonList = JsonConvert.DeserializeObject<List<SearchAllRoadNames>>(jsonString);
// Filter the list based on the specified type and road name filter
string roadNameFilter = value;
var filteredList = jsonList.Where(x => x.roadName.StartsWith(roadNameFilter)).Select(x => x.roadName).ToList();
return filteredList;
}
}
}
谢谢大家。