c# - 无法反序列化当前的 JSON 数组(例如 [1,2,3])& {“解析值时遇到意外字符:e. Path '', line 0, position 0.”}
问题描述
我看到很多类似的问题,但这次我真的无法解决。我遇到的问题是用某个对象的内容填充视图以更新它。当我按下“编辑”时,理论上,所有字段都应该自动插入。到目前为止我尝试了什么?
public ActionResult CreateOrEdit(int id = 0)
{
if (id==0)
return View(new Recipe());
else
{
HttpResponseMessage response = GlobalVariables.client.GetAsync(id.ToString()).Result;
//return ViewJsonConvert.DeserializeObject<IList<Recipe>>(response.ToString()));
return View(response.Content.ReadAsAsync<Recipe>().Result);
//return View(new Recipe());
}
}
return View(response.Content.ReadAsAsync<Recipe>().Result);
- 当使用它作为回报时,我收到这个错误:
无法反序列化当前的 JSON 数组(例如 [1,2,3])
查找问题后,我尝试了以下操作:
return View(JsonConvert.DeserializeObject<IList<Recipe>>(response.ToString()));
这让我遇到了这个错误:
{"解析值时遇到意外字符:s.Path '', line 0, position 0."}
在这一点上,我被卡住了。我假设它正在尝试反序列化以下 JSON:
{
"id": 5002,
"name": "Test Recipe",
"recipeLink": "testlink",
"category1Id": 7757,
"category2Id": 7758,
"category3Id": 7759,
"category4Id": 7760,
"recipeById": 1,
"totalTime": 30,
"totalTimeUnitId": 1,
"activeTime": 20,
"activeTimeUnitId": 1,
"instructions": "Test Instructions",
"sourceKey": "Test SK",
"recipeBy": "TestPerson",
"insertedAtUtc": "2019-09-04T12:18:48.0466667",
"isVerified": 1,
"numPersons": 5
}
如果需要,这里是处理操作的 API 控制器的代码。
[Route("v1/recipe/{id}")]
[HttpPut()]
public IActionResult UpdateList(int id, [FromBody]Recipe recipe)
{
var category1Id = 7757;
var category2Id = 7758;
var category3Id = 7759;
var category4Id = 7760;
var isVerified = 0;
var recipeBy = "TestPerson";
var recipeById = 1;
try
{
if (recipe == null) throw new ArgumentException("No data specified");
//if (newData.Name == null) throw new ArgumentException("No name specified");
using (var con = _connFactory())
{
con.Execute(@"UPDATE dbo.Recipe SET Name=@name, RecipeLink=@recipeLink, Category1Id=@category1Id ,Category2Id=@category2Id,
Category3Id=@category3Id, Category4Id=@category4Id, RecipeById=@recipeById, TotalTime=@totalTime, TotalTimeUnitId=@totalTimeUnitId,
ActiveTime=@activeTime, ActiveTimeUnitId=@activeTimeUnitId, Instructions=@instructions, SourceKey=@sourceKey, RecipeBy=@recipeBy,
InsertedAtUtc=getutcdate(), IsVerified=@isVerified, NumPersons=@numPersons WHERE Id=@id",
new
{
id,
recipe.name,
recipe.recipeLink,
category1Id,
category2Id,
category3Id,
category4Id,
recipeById,
recipe.totalTime,
recipe.totalTimeUnitId,
recipe.activeTime,
recipe.activeTimeUnitId,
recipe.instructions,
recipe.sourceKey,
recipeBy,
isVerified,
recipe.numPersons
});
}
return Ok(recipe);
}
catch (Exception exc)
{
return BadRequest();
}
}
解决方案
我相信您的response
字符串以 Unicode 字节顺序标记字符开头。
例如...
using Newtonsoft.Json;
namespace StackOverflow
{
class MainClass
{
public static void Main(string[] args)
{
var json = @"{
'id': 5002,
'name': 'Test Recipe',
'recipeLink': 'testlink',
'category1Id': 7757,
'category2Id': 7758,
'category3Id': 7759,
'category4Id': 7760,
'recipeById': 1,
'totalTime': 30,
'totalTimeUnitId': 1,
'activeTime': 20,
'activeTimeUnitId': 1,
'instructions': 'Test Instructions',
'sourceKey': 'Test SK',
'recipeBy': 'TestPerson',
'insertedAtUtc': '2019-09-04T12:18:48.0466667',
'isVerified': 1,
'numPersons': 5
}".Replace("'", "\"");
//This works...
var deserialized1 = JsonConvert.DeserializeObject(json);
//Prepend a U+FEFF Byte Order Mark character...
json = "\uFEFF" + json;
//This fails with error:
//Newtonsoft.Json.JsonReaderException:
//Unexpected character encountered while parsing value: . Path '', line 0, position 0.
var deserialized2 = JsonConvert.DeserializeObject(json);
}
}
}
推荐阅读
- python - 如何解决或识别神经网络中的问题,可以在 Tensorboard Graphs 上看到
- definition - Nearley 标记器与规则
- php - 在 Woocommerce 3 中添加 3 个结帐选择字段作为复合日期字段
- coq - Coq:当只有一种情况时对 Prop for Set 执行反转
- python - Python 和队列文件管理
- asp.net - 引导弹出框内的 asp 复选框 oncheckedchanged 未触发
- javascript - 用零数据填充日志
- python - Tweepy error code 400
- javascript - puppeteer : how check if browser is still open and working
- amazon-web-services - Amazon EC2 - How to execute the 'launch" part of a Cross-Account AMI Copy?