- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我面临一个关于 GSON json 到 Java 的问题。我在这里查找了很多帖子,但找不到我的问题的解决方案。所以我在这里列出了我的问题。我正在尝试获取 json 中有一个 Map 的数据,但我无法检索数据。在我的日志中,我可以看到只有这么多数据出现然后它抛出异常。有人请指导我通过。非常感谢!
这是我从我正在处理的 Android 应用程序中点击 URL 的 Json 数据
{
"success" : true,
"messages" : {
"success" : [
"SEARCH_QUERY_SUCCESS"
]
},
"session" : {
"id" : "cn694ivr8bmqnrveh9n8841oh7",
"expire" : "",
"YII_CSRF_TOKEN" : "4fa0ae103b547836241f5278311839b407050919"
},
"metadata" : {
"product_count" : "4458",
"category_ids" : "3",
"results" : [{
"id" : "105089",
"data" : {
"sku" : "MA851AA10ZLX",
"name" : "Alexa Mid Rise Super Skinny Leg",
"new-product" : false,
"url" : "http:\/\/theiconic.bugfoot.de\/mobile-api\/Alexa-Mid-Rise-Super-Skinny-Leg-105089.html",
"simples" : {
"MA851AA10ZLX-406437" : {},
"MA851AA10ZLX-406438" : {},
"MA851AA10ZLX-406439" : {},
"MA851AA10ZLX-406440" : {},
"MA851AA10ZLX-406441" : {},
"MA851AA10ZLX-406442" : {},
"MA851AA10ZLX-406443" : {},
"MA851AA10ZLX-406444" : {
"meta" : {
"sku" : "MA851AA10ZLX-406444",
"price" : "149.99",
"caching_hash" : "78ddaaf930f8bd0e0bf595c25643683d",
"shipment_cost_item" : "0.00",
"shipment_cost_order" : "0.00",
"tax_percent" : "10.00",
"quantity" : "2",
"cost" : "64.09",
"size_brand" : "W31\/L34",
"size" : "W31\/L34",
"size_position" : "200",
"3hours_shipment_available" : true,
"estimated_delivery" : "",
"estimated_delivery_position" : ""
},
"attributes" : {
"sort_order" : "0",
"size" : "W31\/L34"
}
}
}
}
}
]
}
}
这是我用于解析的网络类
String jsonString = null;
try {
HttpGet httppost = new HttpGet(URL);
HttpClient httpClient = new DefaultHttpClient();
if (httpClient != null) {
HttpResponse response = httpClient.execute(httppost);
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
jsonString = reader.readLine();
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(jsonString);
if (jsonString != null) {
Bean obj=new Gson().fromJson(jsonString, Bean.class);
Message msg = new Message();
msg.obj = obj;
responseHandler.sendMessage(msg);
//obj can be sent to a handler
}
}
}
这是包含 Map 的 Simples Bean 类
public class Simples {
private Map<String, KeyMap> keyMap;
public Map<String, KeyMap> getKeyMap() {return keyMap;}
public void setKeyMap(Map<String, KeyMap> keyMap) {this.keyMap = keyMap;}}
这是我的 KeyMap Bean 类
public class KeyMap {
private Meta meta;
private Attributes attributes;
public Meta getMeta() {
return meta;
}
public void setMeta(Meta meta) {
this.meta = meta;
}
public Attributes getAttributes() {
return attributes;
}
public void setAttributes(Attributes attributes) {
this.attributes = attributes;
}
}
最佳答案
实际上你得到了错误 BEGIN_OBJECT 但是是 STRING
因为 gson 期待对象而不是字符串,而且它是 json 中的一个对象,你错误地将你的类映射到 Bean 类中,而你没有在此处发布 Bean,简单类胶水代码。
您的 JSON 模型类可以像这样映射
public class ProductInfo {
private boolean success;
private Map<String, String[]> messages;
private SessionData session;
private MetaData metadata;
}
public class SessionData {
private String id;
private String expire;
private String YII_CSRF_TOKEN;
}
public class MetaData {
private String product_count;
private String category_ids;
private List<Result> results;
}
public class Result {
private String id;
private Data data;
}
public class Data {
private String sku;
private String name;
@SerializedName(value = "new-product")
private String newProduct;
private String url;
Map<String, KeyMap> simples;
}
public class KeyMap {
private Meta meta;
private Attributes attributes;
}
public class Meta {
private String sku;;
private String price;
private String caching_hash;
private String shipment_cost_item;
private String shipment_cost_order;
private String tax_percent;
private String quantity;
private String cost;
private String size_brand;
private String size;
private String size_position;
@SerializedName(value = "3hours_shipment_available")
private String hours_shipment_available;
private String estimated_delivery;
private String estimated_delivery_positio;
}
public class Attributes {
private String sort_order;
private String size;
}
最后反序列化
ProductInfo productInfo = gson.fromJson(reader, ProductInfo.class);
享受 :)
关于java.lang.IllegalStateException : Expected BEGIN_OBJECT but was STRING at line 1 column 62 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20376207/
这正确地呈现了标题,因为我可以看到一个列带有“Product ID”标题的表:。我正试图重构它,这样我就可以重用它,因为它们在整个应用程序中几乎是相同的:。在这种情况下,头不会呈现...检查页面元素中
我什至不知道如何表达这一点,但在 Python 中有没有一种方法可以引用等号之前的文本,而无需实际再次编写? ** 编辑 - 我在 Jupyter 中使用 python3 我似乎用了半辈子的时间来写作
假设我有一个包含以下列的字典 dict_ = [ {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', '
假设我有一个包含以下列的字典 dict_ = [ {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', '
假设我的数据库中有一张地铁 map ,其中每条线路的每个站点都是一行。如果我想知道我的线路在哪里互连: mysql> SELECT LineA.stop_id FROM LineA, LineB WH
代码 select c1,c2,c3,c4,c5,c6 from table where c5 in ('a', 'b') 从这里开始,我想将 c5 列拆分为两列,然后根据它们对 c
我正在尝试搜索表格中的两列,即标题和描述。但我想先搜索标题,然后再搜索描述。所以匹配标题的所有行排在第一位,所有匹配描述的行排在第二位 我可以使用单个 SQL 查询来实现吗? 最佳答案 您还可以使用
下面有以下 Python 数据框。 “标志”字段是我想要用代码创建的所需列。 我想要执行以下操作: 如果“分配类型”是预测的并且“Activities_Counter”大于 10,我想创建一个名为“F
我有两列,area 和 block,其中 area 是一个 int 类型,block 是一个 varchar。 现在我正在写两个查询: select * from table where area a
使用 Slick 2,我试图生成一个带有元组 IN 子句的查询: select * from my_table where (a, b) IN ((1, 87)); 给定: val seq: Seq[
我正在尝试从数据透视表中获取一组值,其中 A 列等于值数组,例如 ID 12 的 attribute_value_id 等于 3 和 9。这可以做到吗?我已经走了这么远... ID | post_id
我找不到这样做的有效方法。我在 Python 中有以下 DataFrame,列从 A 到 Z A B C ... Z 0 2.0 8.0 1.0 ... 5.0 1
我的数据框中有以下格式的数据: >>> df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD')) >>> df
我有多个与我公司销售的产品相关的表被新产品取代,随着时间的推移,这导致了多个表的出现。 我一遍又一遍地使用以下查询,直到最终表中只剩下 2 个产品... CREATE TABLE mar15a
我有如下 2 个表:- 表A ------------------------------- | product_id | price | --------------------
我有一个名为 tbl_mainsheet7 的表,创建方式如下: pk_mainsheet client_id project_id mainsheet_id project_cat EA_
我得到了以下 HTML 结构: ... ... 我的 CSS: #main-container { width:80%; margin:20px auto;
对于我的以下要求,我无法获得解决方案。 如果 data.table(如下)在 Col1 和 Col3 中有匹配的值。替换 Col2 值(旧的 New-Val)。 Col1 Col2 Col3
我正在通过连接几个表来构建一个 View ,以通过 Entity Framework 提取数据。由于此 View 没有唯一列,EntityFramework 无法检索正确的结果集(即第一列重复)。 为
好的,我已经尝试了太久了,是时候寻求帮助了。我有一个看起来有点像这样的数据框: person fruit quantity all_fruits 0 p1 grapes 2
我是一名优秀的程序员,十分优秀!