我正在使用Newtonsoft Json.net来解析JSON字符串.我将字符串转换为JObject.当通过键访问元素的值时,我希望比较是不区分大小写的.在下面的代码中,我使用"FROM"作为键.我希望它在行json ["FROM"]返回字符串"1".ToString().但它失败了.是否可以使下面的代码工作?
String ptString = "{from: 1, to: 3}";
var json = (JObject)JsonConvert.DeserializeObject(ptString);
String f = json["FROM"].ToString();
Run Code Online (Sandbox Code Playgroud) 在尝试使用javascript手动格式化我的JSON数据并且失败后,我意识到可能有更好的方法.以下是C#中Web服务方法和相关类的代码:
[WebMethod]
public Response ValidateAddress(Request request)
{
return new test_AddressValidation().GenerateResponse(
test_AddressValidation.ResponseType.Ambiguous);
}
...
public class Request
{
public Address Address;
}
public class Address
{
public string Address1;
public string Address2;
public string City;
public string State;
public string Zip;
public AddressClassification AddressClassification;
}
public class AddressClassification
{
public int Code;
public string Description;
}
Run Code Online (Sandbox Code Playgroud)
Web服务使用SOAP/XML很有效,但我似乎无法使用javascript和jQuery获得有效的响应,因为我从服务器返回的消息与我的手工编码的JSON有问题.
我不能使用jQuery getJSON
函数,因为请求需要HTTP POST,所以我使用的是低级ajax
函数:
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://bmccorm-xp/HBUpsAddressValidation/AddressValidation.asmx/ValidateAddress",
data: "{\"Address\":{\"Address1\":\"123 Main Street\",\"Address2\":null,\"City\":\"New York\",\"State\":\"NY\",\"Zip\":\"10000\",\"AddressClassification\":null}}",
dataType: "json",
success: function(response){
alert(response); …
Run Code Online (Sandbox Code Playgroud)