我想从我的sqlite数据库中的数据解析json。编码JSON(安卓)
输出我想:
{
"report":{
"product":[
{
"size": "100"
},
{
"size": "200"
}
]
}
}
输出我得到:
{
"report":{
"product":[
{
"size": "200"
},
{
"size": "200"
}
]
}
}
我的代码:
Data = new JSONObject();
Product = new JSONObject();
Report = new JSONObject();
SizeList = controller.distinctSize();
for (HashMap<String, String> map : SizeList){
for(Entry<String, String> mapEntry : map.entrySet()){
String key = mapEntry.getKey();
Object value = mapEntry.getValue();
Report.accumulate("product", Product);
Product.put(key, value); //size
}
}
Data.put("report", Report);
你能告诉我如何解决我的代码,这样我可以得到我想要的数据?
[[已解决]]
我还有一个问题。由于代码现在循环,我很难解析它。
输出我现在想:
{
"report":{
"report-date":"19-03-2013",
"period-start":"18-03-2013",
"period-end":"22-03-2013",
"products":[
{
"commodity":"123456789",
"size":"155g",
"prices":{
"est_id_1":"12.60",
"est_id_3":"13.45",
}
},
{
"commodity":"234567890",
"size":"155g",
"prices":{
"est_id_1":"3.5",
}
}
]
}
}
但我从我的代码得到的是:
{
"report":{
"report-date":"19-03-2013",
"period-start":"18-03-2013",
"period-end":"22-03-2013",
"products":[
{
"commodity":"123456789",
"size":"155g",
"prices":{
"est_id_1":"12.60",
//missing one establishment here
}
},
{
"commodity":"234567890",
"size":"155g",
"prices":{
"est_id_1":"3.5",
}
}
]
}
}
我的代码:
try{
Data = new JSONObject();
Report = new JSONObject();
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
int i=0;
SizeList = controller.distinctSize();
for (HashMap<String, String> map : SizeList){
for(Entry<String, String> mapEntry : map.entrySet()){
String key = mapEntry.getKey();
Object value = mapEntry.getValue();
Product = new JSONObject();
Product.put(key, value); //size
ComList = controller.distinctCommodity((String) value);
for (HashMap<String, String> map1 : ComList){
for(Entry<String, String> mapEntry1 : map1.entrySet()){
i++;
String key1 = mapEntry1.getKey();
Object value1 = mapEntry1.getValue();
if(i>1){
Product = new JSONObject();
Report.accumulate("product", Product);
Product.put(key1, value1); //commodity
Product.put(key, value);
}
else{
Report.accumulate("product", Product);
Product.put(key1, value1); //commodity
}
EstabList = controller.establishment((String) value, (String) value1);
for (HashMap<String, String> map2 : EstabList){
for(Entry<String, String> mapEntry2 : map2.entrySet()){
String key2 = mapEntry2.getKey();
Object value2 = mapEntry2.getValue();
PriceList = controller.price((String) value, (String) value1, (String) value2);
for (HashMap<String, String> map3 : PriceList){
Price = new JSONObject();
Product.put("prices", Price);
for(Entry<String, String> mapEntry3 : map3.entrySet()){
String key3 = mapEntry3.getKey();
Object value3 = mapEntry3.getValue();
Price.put((String) value2, value3); //estabid and price
}
}
}
}
}
}
}
}
Report.accumulate("product",Product);
Report.put("report-date", dateFormat.format(date));
DateList = controller.dates();
for (HashMap<String, String> map : DateList){
for(Entry<String, String> mapEntry : map.entrySet()){
String key = mapEntry.getKey();
Object value = mapEntry.getValue();
Report.accumulate(key, value);
}
}
Data.put("report", Report);
我应该怎么做呢?
欢迎您。请将此答案标记为接受以帮助其他人发生类似问题 – Blackbelt 2013-05-14 07:22:16
我已经做了:) – frustratedmonkey 2013-05-14 07:29:09
嗨,我还可以问你关于这件事吗?当然是 – frustratedmonkey 2013-05-14 08:31:38