本文介绍了Android的JSON解析并存储到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想提出一个应用程序有一个数据库,现在我试图解析的数据值,从这样的:
I am making an app which have a database and now I am trying to parse data values from this:
<stringxmlns="http://layanan.mobilefin/">{
"kondisi": [
{
"Condition": "New",
"Kondisi": "Baru"
},
{
"Condition": "Used",
"Kondisi": "Bekas"
}
],
"Zona": [
{
"Zona": "Zona1",
"Zone": "Zone1"
},
{
"Zona": "Zona2",
"Zone": "Zone3"
}
],
"tipeAsuransi": [
{
"Type": "Combine"
},
{
"Type": "Comprehensive"
},
{
"Type": "TLO"
}
],
"Erate": [
{
"id": "1",
"Kondisi": "Baru",
"Condition": "New",
"Tenor": "12",
"ERate": "12",
"ERateDB": "11.5"
},
{
"id": "10",
"Kondisi": "Bekas",
"Condition": "Used",
"Tenor": "60",
"ERate": "13.5",
"ERateDB": "12.9"
},
{
"id": "2",
"Kondisi": "Baru",
"Condition": "New",
"Tenor": "24",
"ERate": "12.2",
"ERateDB": "11.6"
},
{
"id": "3",
"Kondisi": "Baru",
"Condition": "New",
"Tenor": "36",
"ERate": "12.3",
"ERateDB": "11.7"
},
{
"id": "4",
"Kondisi": "Baru",
"Condition": "New",
"Tenor": "48",
"ERate": "12.4",
"ERateDB": "11.8"
},
{
"id": "5",
"Kondisi": "Baru",
"Condition": "New",
"Tenor": "60",
"ERate": "12.5",
"ERateDB": "11.9"
},
{
"id": "6",
"Kondisi": "Bekas",
"Condition": "Used",
"Tenor": "12",
"ERate": "13",
"ERateDB": "12.5"
},
{
"id": "7",
"Kondisi": "Bekas",
"Condition": "Used",
"Tenor": "24",
"ERate": "13.2",
"ERateDB": "12.6"
},
{
"id": "8",
"Kondisi": "Bekas",
"Condition": "Used",
"Tenor": "36",
"ERate": "13.3",
"ERateDB": "12.7"
},
{
"id": "9",
"Kondisi": "Bekas",
"Condition": "Used",
"Tenor": "48",
"ERate": "13.4",
"ERateDB": "12.8"
}
],
"Arate": [
{
"id": "1",
"Zona": "1",
"Tenor": "12",
"TLORate": "1.1",
"CompRate": "2.1",
"CombRate": "3.1"
},
{
"id": "10",
"Zona": "2",
"Tenor": "60",
"TLORate": "2.0",
"CompRate": "3.0",
"CombRate": "4.0"
},
{
"id": "11",
"Zona": "3",
"Tenor": "12",
"TLORate": "2.1",
"CompRate": "3.1",
"CombRate": "4.1"
},
{
"id": "12",
"Zona": "3",
"Tenor": "24",
"TLORate": "2.2",
"CompRate": "3.2",
"CombRate": "4.2"
},
{
"id": "13",
"Zona": "3",
"Tenor": "36",
"TLORate": "2.3",
"CompRate": "3.3",
"CombRate": "4.3"
},
{
"id": "14",
"Zona": "3",
"Tenor": "48",
"TLORate": "2.4",
"CompRate": "3.4",
"CombRate": "4.4"
},
{
"id": "15",
"Zona": "3",
"Tenor": "60",
"TLORate": "2.5",
"CompRate": "3.5",
"CombRate": "4.5"
},
{
"id": "2",
"Zona": "1",
"Tenor": "24",
"TLORate": "1.2",
"CompRate": "2.2",
"CombRate": "3.2"
},
{
"id": "3",
"Zona": "1",
"Tenor": "36",
"TLORate": "1.3",
"CompRate": "2.3",
"CombRate": "3.3"
},
{
"id": "4",
"Zona": "1",
"Tenor": "48",
"TLORate": "1.4",
"CompRate": "2.4",
"CombRate": "3.4"
},
{
"id": "5",
"Zona": "1",
"Tenor": "60",
"TLORate": "1.5",
"CompRate": "2.5",
"CombRate": "3.5"
},
{
"id": "6",
"Zona": "2",
"Tenor": "12",
"TLORate": "1.6",
"CompRate": "2.6",
"CombRate": "3.6"
},
{
"id": "7",
"Zona": "2",
"Tenor": "24",
"TLORate": "1.7",
"CompRate": "2.7",
"CombRate": "3.7"
},
{
"id": "8",
"Zona": "2",
"Tenor": "36",
"TLORate": "1.8",
"CompRate": "2.8",
"CombRate": "3.8"
},
{
"id": "9",
"Zona": "2",
"Tenor": "48",
"TLORate": "1.9",
"CompRate": "2.9",
"CombRate": "3.9"
}
]
}</string>
我收到因为这个错误而转换的JSON对象,&LT; stringxmlns =HTTP://layanan.mobilefin/&GT; ......&LT; /串&GT;
在logcat的:
04-22 10:14:29.631: D/dalvikvm(366): GC_EXTERNAL_ALLOC freed 120K, 49% free 2894K/5639K, external 3101K/3366K, paused 419ms
04-22 10:14:31.962: D/Response:(366): > <?xml version="1.0" encoding="utf-8"?>
04-22 10:14:31.962: D/Response:(366): <string xmlns="http://layanan.mobilefin/">{
04-22 10:14:31.962: D/Response:(366): "kondisi": [
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "Condition": "New",
04-22 10:14:31.962: D/Response:(366): "Kondisi": "Baru"
04-22 10:14:31.962: D/Response:(366): },
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "Condition": "Used",
04-22 10:14:31.962: D/Response:(366): "Kondisi": "Bekas"
04-22 10:14:31.962: D/Response:(366): }
04-22 10:14:31.962: D/Response:(366): ],
04-22 10:14:31.962: D/Response:(366): "Zona": [
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "Zona": "Zona1",
04-22 10:14:31.962: D/Response:(366): "Zone": "Zone1"
04-22 10:14:31.962: D/Response:(366): },
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "Zona": "Zona2",
04-22 10:14:31.962: D/Response:(366): "Zone": "Zone3"
04-22 10:14:31.962: D/Response:(366): }
04-22 10:14:31.962: D/Response:(366): ],
04-22 10:14:31.962: D/Response:(366): "tipeAsuransi": [
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "Type": "Combine"
04-22 10:14:31.962: D/Response:(366): },
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "Type": "Comprehensive"
04-22 10:14:31.962: D/Response:(366): },
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "Type": "TLO"
04-22 10:14:31.962: D/Response:(366): }
04-22 10:14:31.962: D/Response:(366): ],
04-22 10:14:31.962: D/Response:(366): "Erate": [
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "id": "1",
04-22 10:14:31.962: D/Response:(366): "Kondisi": "Baru",
04-22 10:14:31.962: D/Response:(366): "Condition": "New",
04-22 10:14:31.962: D/Response:(366): "Tenor": "12",
04-22 10:14:31.962: D/Response:(366): "ERate": "12",
04-22 10:14:31.962: D/Response:(366): "ERateDB": "11.5"
04-22 10:14:31.962: D/Response:(366): },
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "id": "10",
04-22 10:14:31.962: D/Response:(366): "Kondisi": "Bekas",
04-22 10:14:31.962: D/Response:(366): "Condition": "Used",
04-22 10:14:31.962: D/Response:(366): "Tenor": "60",
04-22 10:14:31.962: D/Response:(366): "ERate": "13.5",
04-22 10:14:31.962: D/Response:(366): "ERateDB": "12.9"
04-22 10:14:31.962: D/Response:(366): },
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "id": "2",
04-22 10:14:31.962: D/Response:(366): "Kondisi": "Baru",
04-22 10:14:31.962: D/Response:(366): "Condition": "New",
04-22 10:14:31.962: D/Response:(366): "Tenor": "24",
04-22 10:14:31.962: D/Response:(366): "ERate": "12.2",
04-22 10:14:31.962: D/Response:(366): "ERateDB": "11.6"
04-22 10:14:31.962: D/Response:(366): },
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "id": "3",
04-22 10:14:31.962: D/Response:(366): "Kondisi": "Baru",
04-22 10:14:31.962: D/Response:(366): "Condition": "New",
04-22 10:14:31.962: D/Response:(366): "Tenor": "36",
04-22 10:14:31.962: D/Response:(366): "ERate": "12.3",
04-22 10:14:31.962: D/Response:(366): "ERateDB": "11.7"
04-22 10:14:31.962: D/Response:(366): },
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "id": "4",
04-22 10:14:31.962: D/Response:(366): "Kondisi": "Baru",
04-22 10:14:31.962: D/Response:(366): "Condition": "New",
04-22 10:14:31.962: D/Response:(366): "Tenor": "48",
04-22 10:14:31.962: D/Response:(366): "ERate": "12.4",
04-22 10:14:31.962: D/Response:(366): "ERateDB": "11.8"
04-22 10:14:31.962: D/Response:(366): },
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "id": "5",
04-22 10:14:31.962: D/Response:(366): "Kondisi": "Baru",
04-22 10:14:31.962: D/Response:(366): "Condition": "New",
04-22 10:14:31.962: D/Response:(366): "Tenor": "60",
04-22 10:14:31.962: D/Response:(366): "ERate": "12.5",
04-22 10:14:31.962: D/Response:(366): "ERateDB": "11.9"
04-22 10:14:31.962: D/Response:(366): },
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "id": "6",
04-22 10:14:31.962: D/Response:(366): "Kondisi": "Bekas",
04-22 10:14:31.962: D/Response:(366): "Condition": "Used",
04-22 10:14:31.962: D/Response:(366): "Tenor": "12",
04-22 10:14:31.962: D/Response:(366): "ERate": "13",
04-22 10:14:31.962: D/Response:(366): "ERateDB": "12.5"
04-22 10:14:31.962: D/Response:(366): },
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "id": "7",
04-22 10:14:31.962: D/Response:(366): "Kondisi": "Bekas",
04-22 10:14:31.962: D/Response:(366): "Condition": "Used",
04-22 10:14:31.962: D/Response:(366): "Tenor": "24",
04-22 10:14:31.962: D/Response:(366): "ERate": "13.2",
04-22 10:14:31.962: D/Response:(366): "ERateDB": "12.6"
04-22 10:14:31.962: D/Response:(366): },
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "id": "8",
04-22 10:14:31.962: D/Response:(366): "Kondisi": "Bekas",
04-22 10:14:31.962: D/Response:(366): "Condition": "Used",
04-22 10:14:31.962: D/Response:(366): "Tenor": "36",
04-22 10:14:31.962: D/Response:(366): "ERate": "13.3",
04-22 10:14:31.962: D/Response:(366): "ERateDB": "12.7"
04-22 10:14:31.962: D/Response:(366): },
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "id": "9",
04-22 10:14:31.962: D/Response:(366): "Kondisi": "Bekas",
04-22 10:14:31.962: D/Response:(366): "Condition": "Used",
04-22 10:14:31.962: D/Response:(366): "Tenor": "48",
04-22 10:14:31.962: D/Response:(366): "ERate": "13.4",
04-22 10:14:31.962: D/Response:(366): "ERateDB": "12.8"
04-22 10:14:31.962: D/Response:(366): }
04-22 10:14:31.962: D/Response:(366): ],
04-22 10:14:31.962: D/Response:(366): "Arate": [
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "id": "1",
04-22 10:14:31.962: D/Response:(366): "Zona": "1",
04-22 10:14:31.962: D/Response:(366): "Tenor": "12",
04-22 10:14:31.962: D/Response:(366): "TLORate": "1.1",
04-22 10:14:31.962: D/Response:(366): "CompRate": "2.1",
04-22 10:14:31.962: D/Response:(366): "CombRate": "3.1"
04-22 10:14:31.962: D/Response:(366): },
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "id": "10",
04-22 10:14:31.962: D/Response:(366): "Zona": "2",
04-22 10:14:31.962: D/Response:(366): "Tenor": "60",
04-22 10:14:31.962: D/Response:(366): "TLORate": "2.0",
04-22 10:14:31.962: D/Response:(366): "CompRate": "3.0",
04-22 10:14:31.962: D/Response:(366): "CombRate": "4.0"
04-22 10:14:31.962: D/Response:(366): },
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "id": "11",
04-22 10:14:31.962: D/Response:(366): "Zona": "3",
04-22 10:14:31.962: D/Response:(366): "Tenor": "12",
04-22 10:14:31.962: D/Response:(366): "TLORate": "2.1",
04-22 10:14:31.962: D/Response:(366): "CompRate": "3.1",
04-22 10:14:31.962: D/Response:(366): "CombRate": "4.1"
04-22 10:14:31.962: D/Response:(366): },
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "id": "12",
04-22 10:14:31.962: D/Response:(366): "Zona": "3",
04-22 10:14:31.962: D/Response:(366): "Tenor": "24",
04-22 10:14:31.962: D/Response:(366): "TLORate": "2.2",
04-22 10:14:31.962: D/Response:(366): "CompRate": "3.2",
04-22 10:14:31.962: D/Response:(366): "CombRate": "4.2"
04-22 10:14:31.962: D/Response:(366): },
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "id": "13",
04-22 10:14:31.962: D/Response:(366): "Zona": "3",
04-22 10:14:31.962: D/Response:(366): "Tenor": "36",
04-22 10:14:31.962: D/Response:(366): "TLORate": "2.3",
04-22 10:14:31.962: D/Response:(366): "CompRate": "3.3",
04-22 10:14:31.962: D/Response:(366): "CombRate": "4.3"
04-22 10:14:31.962: D/Response:(366): },
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "id": "14",
04-22 10:14:31.962: D/Response:(366): "Zona": "3",
04-22 10:14:31.962: D/Response:(366): "Tenor": "48",
04-22 10:14:31.962: D/Response:(366): "TLORate": "2.4",
04-22 10:14:31.962: D/Response:(366): "CompRate": "3.4",
04-22 10:14:31.962: D/Response:(366): "CombRate": "4.4"
04-22 10:14:31.962: D/Response:(366): },
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "id": "15",
04-22 10:14:31.962: D/Response:(366): "Zona": "3",
04-22 10:14:31.962: D/Response:(366): "Tenor": "60",
04-22 10:14:31.962: D/Response:(366): "TLORate": "2.5",
04-22 10:14:31.962: D/Response:(366): "CompRate": "3.5",
04-22 10:14:31.962: D/Response:(366): "CombRate": "4.5"
04-22 10:14:31.962: D/Response:(366): },
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "id": "2",
04-22 10:14:31.962: D/Response:(366): "Zona": "1",
04-22 10:14:31.962: D/Response:(366): "Tenor": "24",
04-22 10:14:31.962: D/Response:(366): "TLORate": "1.2",
04-22 10:14:31.962: D/Response:(366): "CompRate": "2.2",
04-22 10:14:31.962: D/Response:(366): "CombRate": "3.2"
04-22 10:14:31.962: D/Response:(366): },
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "id": "3",
04-22 10:14:31.962: D/Response:(366): "Zona": "1",
04-22 10:14:31.962: D/Response:(366): "Tenor": "36",
04-22 10:14:31.962: D/Response:(366): "TLORate": "1.3",
04-22 10:14:31.962: D/Response:(366): "CompRate": "2.3",
04-22 10:14:31.962: D/Response:(366): "CombRate": "3.3"
04-22 10:14:31.962: D/Response:(366): },
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "id": "4",
04-22 10:14:31.962: D/Response:(366): "Zona": "1",
04-22 10:14:31.962: D/Response:(366): "Tenor": "48",
04-22 10:14:31.962: D/Response:(366): "TLORate": "1.4",
04-22 10:14:31.962: D/Response:(366): "CompRate": "2.4",
04-22 10:14:31.962: D/Response:(366): "CombRate": "3.4"
04-22 10:14:31.962: D/Response:(366): },
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "id": "5",
04-22 10:14:31.962: D/Response:(366): "Zona": "1",
04-22 10:14:31.962: D/Response:(366): "Tenor": "60",
04-22 10:14:31.962: D/Response:(366): "TLORate": "1.5",
04-22 10:14:31.962: D/Response:(366): "CompRate": "2.5",
04-22 10:14:31.962: D/Response:(366): "CombRate": "3.5"
04-22 10:14:31.962: D/Response:(366): },
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "id": "6",
04-22 10:14:31.962: D/Response:(366): "Zona": "2",
04-22 10:14:31.962: D/Response:(366): "Tenor": "12",
04-22 10:14:31.962: D/Response:(366): "TLORate": "1.6",
04-22 10:14:31.962: D/Response:(366): "CompRate": "2.6",
04-22 10:14:31.962: D/Response:(366): "CombRate": "3.6"
04-22 10:14:31.962: D/Response:(366): },
04-22 10:14:31.962: D/Response:(366): {
04-22 10:14:31.962: D/Response:(366): "id": "7",
04-22 10:14:31.962: D/Response:(366): "Zona": "2",
04-22 10:14:31.962: D/Response:(366): "Tenor": "24",
04-22 10:14:31.962: D/Response:(366): "TLORate": "1.7",
04-22 10:14:31.962: D/Response:(366): "Co
04-22 10:14:32.162: W/System.err(366): org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONObject
04-22 10:14:32.262: W/System.err(366): at org.json.JSON.typeMismatch(JSON.java:107)
04-22 10:14:32.372: W/System.err(366): at org.json.JSONObject.<init>(JSONObject.java:158)
04-22 10:14:32.372: W/System.err(366): at org.json.JSONObject.<init>(JSONObject.java:171)
04-22 10:14:32.372: W/System.err(366): at www.mandiritunasfinancemobile.co.id.a1s2d3f4g5h6j7k8l9.p0.comcom.MainActivity$GetDatas.doInBackground(MainActivity.java:96)
04-22 10:14:32.372: W/System.err(366): at www.mandiritunasfinancemobile.co.id.a1s2d3f4g5h6j7k8l9.p0.comcom.MainActivity$GetDatas.doInBackground(MainActivity.java:1)
04-22 10:14:32.372: W/System.err(366): at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-22 10:14:32.412: W/System.err(366): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
04-22 10:14:32.412: W/System.err(366): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
04-22 10:14:32.432: W/System.err(366): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
04-22 10:14:32.471: W/System.err(366): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
04-22 10:14:32.471: W/System.err(366): at java.lang.Thread.run(Thread.java:1019)
04-22 10:17:06.842: W/KeyCharacterMap(366): No keyboard for id 0
04-22 10:17:06.842: W/KeyCharacterMap(366): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
04-22 10:17:12.582: D/dalvikvm(366): GC_EXPLICIT freed 296K, 51% free 2893K/5895K, external 4577K/5209K, paused 51ms
更新:该方法做些什么来获取数据和存储:
UPDATE: the method i do to get the data and store:
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);
// TODO Auto-generated method stub
JSONArray Erate= json.getJSONArray("Erates");
if(Erate!=null)
{
// looping through All Contacts
for(int i = 0; i < Erate.length(); i++){
JSONObject c = Erate.getJSONObject(i);
Cursor cr = db.rawQuery("select * from `rates` where `id`='"+c.getString("id")+"'",null);
String quiery=
"Insert into rates(id,kondisi,condition,tenor,erate,erateDB)"+
"values("+
c.getString("id")+",'"+
c.getString("....")
+"')";
db.execSQL(quiery);
Log.d("Insert",quiery);
cr.close();}
}
任何建议?如何使它工作?
Any suggestion? how to make it works?
推荐答案
由于普拉卡什
和潘卡Nimgade
,我已经找到了solution.The的解决方案是用替换XML字符串(空)。
Thanks to prakash
and Pankaj Nimgade
, i've found the solution.The solution is to replace the xml string with ""(Empty).
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
jsonStr = jsonStr.replace(
"<?xml version=\"1.0\" encoding=\"utf-8\"?>", "");
jsonStr = jsonStr.replace("<string>", "");
jsonStr = jsonStr.replace("</string>", "");
// Log.d("Response: ", "> " + jsonStr);
这篇关于Android的JSON解析并存储到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!