如何使用Web Api 2接收对象json。 我正确发送对象,但在后端我的对象为空。 见下文。
我的对象JSON
这是我将在请求中发送的JSON对象。
//Start APPIUM(CLI) server
Runtime.getRuntime().exec("cmd.exe /c start cmd.exe /k \"appium -a 0.0.0.0 -p 4723 \"");
Thread.sleep(10000);
URL u = new URL("http://0.0.0.0:4723/wd/hub");
//Give Details of ARD and App under Testing
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(CapabilityType.BROWSER_NAME,"chrome");
dc.setCapability("deviceName","LC4C5Y648825");
dc.setCapability("platformName","android");
dc.setCapability("platformVersion", "4.4.2");
Thread.sleep(8000);
//Create Driver object for AndroidDriver
AndroidDriver driver = new AndroidDriver(u,dc);
Thread.sleep(3000);
driver.get("https://www.gmail.com");
Thread.sleep(5000);
//driver.context("NATIVE_APP");
Thread.sleep(5000);
driver.findElement(By.xpath("//*[text()='More options']")).click();
Thread.sleep(2500);
driver.findElement(By.xpath("//*[text()='Create account']")).click();
Thread.sleep(2500);
driver.findElement(By.name("FirstName")).sendKeys("****");
driver.findElement(By.name("LastName")).sendKeys("****");
driver.findElement(By.name("GmailAddress")).sendKeys("*********");
driver.findElement(By.name("Passwd")).sendKeys("******");
Thread.sleep(3000);
driver.findElement(By.name("PasswdAgain")).sendKeys("******");
Thread.sleep(5000);
WebElement we = driver.findElement(By.xpath("//*[@class='android.view.View'][@text='Month']"));
we.isDisplayed();
we.click();[enter image description here][1]
我的对象。
这是我的映射对象。需要使用请求中收到的JSON对象填充的类。
{
"ItensRateio" : [
{
"TipoColetorCusto" : "1",
"ColetorCusto" : "MRBHAD",
"Valor": "R$ 25.22",
"Descricao": "Rateio do Brasil"
},
{
"TipoColetorCusto" : "1",
"ColetorCusto" : "MRBHAD",
"Valor": "R$ 25.22",
"Descricao": "Rateio do Brasil"
}
]
}
我在WebAPi 2中的方法
public class RateioSimplesRequestModel
{
List<ItemRateio> ItensRateio { get; set; }
List<ItemMaterial> ItensMaterial { get; set; }
public RateioSimplesRequestModel()
{
ItensRateio = new List<ItemRateio>();
ItensMaterial = new List<ItemMaterial>();
}
}
public class ItemRateio
{
public string TipoColetorCusto { get; set; }
public string ColetorCusto { get; set; }
public string Valor { get; set; }
public string Descricao { get; set; }
}
public class ItemMaterial
{
public string CNAE { get; set; }
public string CodigoMaterial { get; set; }
public string Descricao { get; set; }
}
我怎么能完美?
答案 0 :(得分:3)
您的列表不是公共的,json.net默认只映射公共属性。此外,集合不应公开设置。
public class RateioSimplesRequestModel
{
public List<ItemRateio> ItensRateio { get; private set; }
public List<ItemMaterial> ItensMaterial { get; private set; }
public RateioSimplesRequestModel()
{
ItensRateio = new List<ItemRateio>();
ItensMaterial = new List<ItemMaterial>();
}
}
答案 1 :(得分:0)
请注意,您在list
... json对象的属性中拥有class
因此需要正确格式化并且应发送parametro
而不是ItensRateio
}
var parametro = {};
parametro.ItensRateio = [
{
"TipoColetorCusto" : "1",
"ColetorCusto" : "MRBHAD",
"Valor": "R$ 25.22",
"Descricao": "Rateio do Brasil"
},
{
"TipoColetorCusto" : "1",
"ColetorCusto" : "MRBHAD",
"Valor": "R$ 25.22",
"Descricao": "Rateio do Brasil"
}
];
答案 2 :(得分:0)
如果您正在使用C#,验证对象是否可以序列化的一种方法是构造对象并使用 JsonConvert 将其转换为JSON等效对象。这应该突出显示任何成员保护级别问题,并生成预期的JSON。
根据上面的类,这将生成以下JSON:
{
"ItensRateio": [
{
"TipoColetorCusto": "1",
"ColetorCusto": "MRBHAD",
"Valor": "R$ 25.22",
"Descricao": "Rateio do Brasil"
},
{
"TipoColetorCusto": "1",
"ColetorCusto": "MRBHAD",
"Valor": "R$ 25.22",
"Descricao": "Rateio do Brasil"
}
],
"ItensMaterial": []
}