C# 无法将Json反序列化为对象
这是我的Json:C# 无法将Json反序列化为对象,c#,json,deserialization,json-deserialization,C#,Json,Deserialization,Json Deserialization,这是我的Json: {"Username":"Test","ProductIds":"[30, 50]","RouteName":"ABCD"} 这是我的目标: public class SuggestMobileModel { public string Username { get; set; } public List<int> ProductIds { get; set; } public string RouteNa
{"Username":"Test","ProductIds":"[30, 50]","RouteName":"ABCD"}
这是我的目标:
public class SuggestMobileModel
{
public string Username { get; set; }
public List<int> ProductIds { get; set; }
public string RouteName { get; set; }
}
我现在可以做什么来成功地反序列化而不删除代码?Json是由代码生成的,所以它总是带有引号
——编辑---强>
下面是创建该Json字符串的Java代码。有什么错误吗?它使用的是org.json
库
// 1. create HttpClient
HttpClient client = new DefaultHttpClient();
HttpResponse response;
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(urlSuggestRoute);
String json = "";
// 3. build jsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("Username", User.getUsername());
List<Integer> listProductIds = new ArrayList<Integer>();
if (Cart.getCart() != null && Cart.getSize() > 0) {
for (int i = 0; i < Cart.getSize(); i++) {
listProductIds.add(Cart.getCartItem(i)
.getProductAttribute().getProductId());
}
}
jsonObject.accumulate("ProductIds", listProductIds);
jsonObject.accumulate("RouteName", txtRoute.getSelectedItem()
.toString());
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();
// 5. set json to StringEntity
StringEntity se = new StringEntity(json);
// 6. set httpPost Entity
httpPost.setEntity(se);
// 7. Set some headers to inform server about the type of the
// content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
// 8. Execute POST request to the given URL
HttpResponse httpResponse = client.execute(httpPost);
//1。创建HttpClient
HttpClient=new DefaultHttpClient();
HttpResponse响应;
// 2. 向给定URL发出POST请求
HttpPost HttpPost=新的HttpPost(urlSuggestRoute);
字符串json=“”;
// 3. 构建jsonObject
JSONObject JSONObject=新的JSONObject();
累积(“Username”,User.getUsername());
List ListProductId=new ArrayList();
if(Cart.getCart()!=null&&Cart.getSize()>0){
对于(int i=0;i
在数组值周围加双引号是无效的JSON(请参见第页的语言语法)。因此,您的JSON解析器可能不支持这一点
其他背景信息:JSON将
productId
定义为与字符串值[30,50]
在数组值周围加双引号是无效的JSON(请参见第页的语言语法)。因此,您的JSON解析器可能不支持这一点
其他背景信息:您的JSON将
productId
定义为一对字符串值[30,50]
好的,我已经搞定了。在生成Json的代码中,我应该使用JsonArray
而不是List
,那么库不会在数组周围加引号
非常感谢你的帮助:)好的,我已经弄明白了。在生成Json的代码中,我应该使用
JsonArray
而不是List
,那么库不会在数组周围加引号
非常感谢您的帮助:)您使用的是哪个JSON库?目前,我将此JSON发送到Web API控制器,并让它自己反序列化。我也尝试过使用Newtonsoft,但没有帮助。谢谢,但不管怎样都没关系,只是检查了JSON规范。请参阅下面的答案。您确实应该修复生成无效JSON的代码,并将数组用双引号括起来。该值是字符串的有效JSON,而不是值数组。我已经添加了创建该JSON字符串的代码。请看一看。非常感谢您的帮助。您使用的是哪个JSON库?目前,我将此JSON发送到Web API控制器,并让它自己反序列化。我也尝试过使用Newtonsoft,但没有帮助。谢谢,但不管怎样都没关系,只是检查了JSON规范。请参阅下面的答案。您确实应该修复生成无效JSON的代码,并将数组用双引号括起来。该值是字符串的有效JSON,而不是值数组。我已经添加了创建该JSON字符串的代码。请看一看。非常感谢您的帮助。那么,将其定义为整数数组的正确方法是什么呢?但是,就像您那样,没有双引号,例如:
,“productid:[30,50],…
那么,将其定义为整数数组的正确方法是什么呢?就像您那样,没有双引号,例如:。。。,“产品标识”:[30,50],…
// 1. create HttpClient
HttpClient client = new DefaultHttpClient();
HttpResponse response;
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(urlSuggestRoute);
String json = "";
// 3. build jsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("Username", User.getUsername());
List<Integer> listProductIds = new ArrayList<Integer>();
if (Cart.getCart() != null && Cart.getSize() > 0) {
for (int i = 0; i < Cart.getSize(); i++) {
listProductIds.add(Cart.getCartItem(i)
.getProductAttribute().getProductId());
}
}
jsonObject.accumulate("ProductIds", listProductIds);
jsonObject.accumulate("RouteName", txtRoute.getSelectedItem()
.toString());
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();
// 5. set json to StringEntity
StringEntity se = new StringEntity(json);
// 6. set httpPost Entity
httpPost.setEntity(se);
// 7. Set some headers to inform server about the type of the
// content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
// 8. Execute POST request to the given URL
HttpResponse httpResponse = client.execute(httpPost);