我正在创建一个应该接受JSON作为输入参数的Controller。 JSON很简单,如下所示
public class CodeDTO
{
public class RootObject
{
public string Barcode { get; set; }
}
}
我为上面的JSON创建了一个模型类,如
public class SetNameController : ApiController
{
[HttpPost]
public async Task<IHttpActionResult> Get(CodeDTO bCode)
{
var returnObj = JsonConvert.DeserializeObject<CodeDTO.RootObject>(bCode);
string Bar_Code = returnObj.Barcode.ToString();
if (Bar_Code == "" || Bar_Code == null)
{
............
现在,我需要接收此CodeDTO作为输入参数并解析它以找到BarCode
import hashlib
import os.path
from secure import BadCharacterError
def setLayers(filePath, passwords, usernames=None):
# sanity check
if os.path.exists(filePath): raise FileExistsError
if usernames is not None and len(usernames) != len(passwords): raise IndexError
for name in usernames:
if "|" in name: raise BadCharacterError("Username contained bad character '|'!")
# Hash the passwords
counter = 0
for password in passwords:
salt = ""
if usernames is not None: salt = hashlib.sha512(usernames[counter].encode()).hexdigest() # generate a salt
hashedPassword = hashlib.sha512(salt.encode() + password.encode()).hexdigest() # hash them together
passwords[counter] = hashedPassword # replace the list entry
counter += 1
# write them to a file
with open(filePath, "a+") as file:
counter = 0
for password in passwords:
if usernames is None:
file.write(password)
else:
file.write(password + "|" + usernames[counter] + "\n")
counter += 1
def checkLayer(filePath, password, username=None):
# sanity check
if not os.path.exists(filePath): raise FileNotFoundError
# find the password hash
salt = ""
if username is not None: salt = hashlib.sha512(username.encode()).hexdigest() # find the salt
hashedPassword = hashlib.sha512(salt.encode() + password.encode()).hexdigest() # hash them together
with open(filePath, "r") as file:
db = file.read().split("\n")
counter = 0
if username is not None:
for entry in db:
entry = entry.split("|")
dbPassword = entry[0]
dbUsername = entry[1]
if dbPassword == hashedPassword and dbUsername == username:
return counter
counter += 1
elif username is None:
for entry in db:
if hashedPassword == entry:
return counter
counter += 1
return False # we will only reach this point if all of the others were not matched
但它会抛出以下错误,如
最佳重载方法匹配 &#39; Newtonsoft.Json.JsonConvert.DeserializeObject(字符串, params Newtonsoft.Json.JsonConverter [])&#39;有一些无效的论点
如何反序列化JSON并获取元素
答案 0 :(得分:1)
您不需要自己进行反序列化,请按以下步骤进行更改:
public async Task<IHttpActionResult> Get([FromBody] CodeDTO.RootObject bCode)
{
string Bar_Code = bCode.Barcode.ToString();
}
当您发布数据时,请在标头请求中指定内容类型:
contentType:"application/json"