2

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>

I am getting error while converting JSON object, because of this

<stringxmlns="http://layanan.mobilefin/">.......</string>

the 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 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();}
    }

Any suggestion how to make it work?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jedi Fighter
  • 377
  • 1
  • 4
  • 20
  • 1
    Is it JSON data? Your first tag shows that its a xml data. – Piyush Apr 22 '15 at 04:01
  • The above json string replace with empty string like: jsonstr.replace("",""); and jsonstr.replace("",""); then your convert the string to json easily. – prakash Apr 22 '15 at 04:01
  • issues is with the json data itself, you have tags which are used in xml and becaus of which it is not a Json data to begin with, try removing "" – Pankaj Nimgade Apr 22 '15 at 04:02
  • @PiyushGupta yes it is, in the inside . – Jedi Fighter Apr 22 '15 at 04:04
  • @prakash would you please give an example? – Jedi Fighter Apr 22 '15 at 04:04
  • Have you checked that your JSON data is not valid. – Piyush Apr 22 '15 at 04:12
  • @PiyushGupta i know, thats why i asked the question because this is tricky and i got confused – Jedi Fighter Apr 22 '15 at 04:12
  • But the problem is because of that `.......`. So why you need this? This created a JSON data as invalid. Check your data here http://jsonlint.com/ – Piyush Apr 22 '15 at 04:13
  • @PiyushGupta i did checked, thats why i know that this xml is the culprit – Jedi Fighter Apr 22 '15 at 04:22
  • Then try to replace that string with blank space. – Piyush Apr 22 '15 at 04:25
  • I'll highly appreciate an example to do that – Jedi Fighter Apr 22 '15 at 04:27
  • @JediFighter where are you getting the json from? a public data provider or your own provider. if it is your own provider, tell them to send properly formatted json. – dora Apr 22 '15 at 04:28
  • @JediFighter we can never rely on replacing xml tag strings with blank space, because later we may get get a different xml tag strings.That is the reason it is difficult to provide a reliable working example. – dora Apr 22 '15 at 04:30
  • how did you parsing the response without checking the valid JSON or not?.In that case itself you are failure and also study about the JSON parsers. – learner Apr 22 '15 at 04:42
  • @Boopathi using this ServiceHandler sh = new ServiceHandler(); String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET); Log.d("Response: ", "> " + jsonStr); ServiceHandler is a class that have similiar function like JSONParser on JSON examples out there – Jedi Fighter Apr 22 '15 at 04:46
  • @all anybody know how to skip the xml? – Jedi Fighter Apr 22 '15 at 06:46
  • @JediFighter i have tried to give code which will skip the invalid part of JSON response.. please check my answer – Kushal Apr 22 '15 at 07:17

3 Answers3

2

I think logic to make substring from JSON response url and get valid JSON response

The code looks like :

int start = url.indexOf("{");   // index from where valid json starts
int end = url.lastIndexOf("}"); // index where valid json completes

// get valid json string by substring method
String validJson = url.substring( start, end+1);
Kushal
  • 8,100
  • 9
  • 63
  • 82
  • what does end+1 mean? – Jedi Fighter Apr 22 '15 at 07:30
  • end +1 is next index after index of `}`... as per `substring` method, end index will be exclusive.. so we should give index+1 there – Kushal Apr 22 '15 at 07:32
  • no,it isnt. Or maybe i use it in a wrong way. What i tried -->>ServiceHandler sh = new ServiceHandler(); String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);<<-- then String validJson = jsonStr.substring( start, end+1); and it gives an error Caused by: java.lang.StringIndexOutOfBoundsException – Jedi Fighter Apr 23 '15 at 00:02
  • what is value of `start` and `end` integer ? – Kushal Apr 23 '15 at 04:25
  • these int start = url.indexOf("{"); int end = url.lastIndexOf("}"); i do as the code above – Jedi Fighter Apr 23 '15 at 05:46
  • Tell their return values in `int` type.. Put Logs in your code and check what value are you getting in `start` and `end` – Kushal Apr 23 '15 at 05:47
  • actually the problem solved by some suggestion under my post comment, but this can be alternative, because replacing method is deprecated(eclipse not saying it is but i ve seen through some posts) – Jedi Fighter Apr 23 '15 at 07:29
  • The solution works fine for the invalid JSON.. If you know other approach, share here – Kushal Apr 23 '15 at 10:08
  • If you have found solution then post it.. or If this solution helped you then accept it – Kushal Apr 26 '15 at 14:13
0

Instead of using Json Parser, try Gson library to convert from Json to String and vice versa. For a thorough example, see this answer. You can create a class with all the information you need and then serialize it with Gson to obtain a perfectly formatted Json file format. Works like a charm.

Community
  • 1
  • 1
Cris
  • 2,002
  • 4
  • 30
  • 51
0

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);
Jedi Fighter
  • 377
  • 1
  • 4
  • 20