我正在创建一个拥有数据库的应用程序,现在我正在尝试解析数据值:
<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对象时遇到错误,因为这个
<stringxmlns="http://layanan.mobilefin/">.......</string>
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
更新:我采取的方法来获取数据和存储:
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 query = "Insert into rates(id, kondisi, condition, tenor, erate, erateDB)"+
"values("+
c.getString("id")+",'"+
c.getString("....")
+"')";
db.execSQL(query);
Log.d("Insert", query);
cr.close();}
}
有任何建议如何使其发挥作用?
答案 0 :(得分:0)
不使用Json Parser,而是尝试将Gson库从Json转换为String,反之亦然。有关详细示例,请参阅此answer。 您可以使用所需的所有信息创建一个类,然后使用Gson将其序列化以获得格式完美的Json文件格式。像魅力一样。
答案 1 :(得分:0)
感谢prakash
和Pankaj Nimgade
,我找到了解决方案。解决方案是用“”(空)替换xml字符串。
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);