2017-06-23 6 views
-1

Hier ist meine JSONorg.json.JSONException: Erwartete wörtliche Wert auf Charakter 0 von

{"result1":[{"pid":"55708","pname":"johnson","unitprice":"42"},{"pid":"16592","pname":"Hamam Soap","unitprice":"30"},{"pid":"02931","pname":"Santoor Soap","unitprice":"26"},{"pid":"71036","pname":"Vivel Soap","unitprice":"50"},{"pid":"25942","pname":"Lux","unitprice":"28"},{"pid":"22658","pname":"Lux","unitprice":"28"},{"pid":"66156","pname":"Olive","unitprice":"50"},{"pid":"16004","pname":"Navaratna Oil","unitprice":"65"},{"pid":"44752","pname":"Lays","unitprice":"20"},{"pid":"62542","pname":"Mysore Sandel","unitprice":"70"},{"pid":"78276","pname":"Chik Shampoo","unitprice":"3"},{"pid":"12629","pname":"Head & shoulders","unitprice":"3"},{"pid":"23524","pname":"ThumsUp","unitprice":"20"},{"pid":"38026","pname":"Dettol Soap","unitprice":"30"}]} 

Es ist gültig JSON, aber ich bin mit dem gleichen Fehler.

 jsonObject=new JSONObject(json_string1); 
     jsonArray=jsonObject.getJSONArray("result1"); 

     while (count<jsonArray.length()) 
     { 
      JSONObject jo=jsonArray.getJSONObject(count); 
      pid=jo.getString("pid"); 
      Product_Name=jo.getString("pname"); 
      product_Price=jo.getString("unitprice"); 
     } 
+0

Können Sie die Fehlermeldung hinzufügen? – Asew

Antwort

0

Sie müssen Zählvariable wie unten erhöhen.

int count = 1; 
jsonObject=new JSONObject(json_string1); 
     jsonArray=jsonObject.getJSONArray("result1"); 

     while (count<jsonArray.length()) 
     { 
      JSONObject jo=jsonArray.getJSONObject(count); 
      pid=jo.getString("pid"); 
      Product_Name=jo.getString("pname"); 
      product_Price=jo.getString("unitprice"); 
      count ++; 
     } 

Fehler ist hier wie

Erwartete wörtliche Wert auf Charakter 0 von/n { "result1": [{ "pid": "55708", "pname": "Johnson", "Einzelpreis" : "42"}, {"pid": "16592", "pname": "Hamam Seife", "unitprice": "30"}, {"pid": "02931", "pname": "Santoor Seife" , "unitprice": "26"}, {"pid": "71036", "pname": "Vivel Soap", "Einheitspreis": "50"}, {"pid": "25942", "pname": "Lux", "Einheitspreis": "28"}, {"pid": "22658", "pname": "Lux", "Einheitspreis": "28"}, {"pid": "66156", "pname ": Olive", "Einheitspreis": "50"}, {"pid": "16004", "pname": "Navaratna Oil", "Einheitspreis": "65"}, {"pid": "44752" , "pname": "Laien", "unitprice": "20"}, {"pid": "62542", "pname": "Mysore Sandel", "Einheitspreis": "70"}, {"pid": "78276", "pname": "Chik Shampoo", "Einheitspreis": "3"}, {"pid": "12 629 "," pname ":" Kopf & Schultern "," Einheitspreis ":" 3 "}, {" pid ":" 23524 "," pname ":" ThumsUp "," Einheitspreis ":" 20 "}, {" pid ":" 38026" , "pname": "Dettol Soap", "Einzelpreis": "30"}]}/n

+0

Danke u Für deine Antwort habe ich den Zählwert mit 0 wie count = 0 initialisiert; –

+0

Funktioniert es? Ist dein Problem gelöst? –

+0

Nein, es funktioniert nicht @Mehul Kabaria –

0

Dies funktioniert gut für mich:

JSONObject jsonObject = new JSONObject("{\"result1\":[{\"pid\":\"55708\",\"pname\":\"johnson\",\"unitprice\":\"42\"},{\"pid\":\"16592\",\"pname\":\"Hamam Soap\",\"unitprice\":\"30\"},{\"pid\":\"02931\",\"pname\":\"Santoor Soap\",\"unitprice\":\"26\"},{\"pid\":\"71036\",\"pname\":\"Vivel Soap\",\"unitprice\":\"50\"},{\"pid\":\"25942\",\"pname\":\"Lux\",\"unitprice\":\"28\"},{\"pid\":\"22658\",\"pname\":\"Lux\",\"unitprice\":\"28\"},{\"pid\":\"66156\",\"pname\":\"Olive\",\"unitprice\":\"50\"},{\"pid\":\"16004\",\"pname\":\"Navaratna Oil\",\"unitprice\":\"65\"},{\"pid\":\"44752\",\"pname\":\"Lays\",\"unitprice\":\"20\"},{\"pid\":\"62542\",\"pname\":\"Mysore Sandel\",\"unitprice\":\"70\"},{\"pid\":\"78276\",\"pname\":\"Chik Shampoo\",\"unitprice\":\"3\"},{\"pid\":\"12629\",\"pname\":\"Head & shoulders\",\"unitprice\":\"3\"},{\"pid\":\"23524\",\"pname\":\"ThumsUp\",\"unitprice\":\"20\"},{\"pid\":\"38026\",\"pname\":\"Dettol Soap\",\"unitprice\":\"30\"}]}"); 
      JSONArray jsonArray = jsonObject.getJSONArray("result1"); 
      int count = 0; 

      String pid="", Product_Name="",product_Price=""; 
      while (count<jsonArray.length()) 
      { 
       JSONObject jo=jsonArray.getJSONObject(count); 
       pid=jo.getString("pid"); 
       Product_Name=jo.getString("pname"); 
       product_Price=jo.getString("unitprice"); 
       count++; 
       System.out.println(pid + " " + Product_Name +" "+ product_Price); 
      } 

Aber seien Sie vorsichtig mit was du gerade machst. Ihre while Schleife durchläuft das gesamte result1-Array. So haben Ihre Variablen pidProduct_nameproduct_Price den Wert des letzten Eintrags des Arrays.

+0

Es funktioniert nicht für mich @Asew –

+0

@SangeethKumar Was funktioniert nicht? Kannst du das Problem ein wenig klar erklären? Weil dieser Code für mich gut kompiliert. – Asew

+0

es zeigt nicht die Ausgabe auch wenn ich Dubug den Code sein direkt von jsonObject springen = neue JSONObject (json_string1); Block fangen und die Exception wie klug anzeigen. –

Verwandte Themen