这是我的JSON
{"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"}]}
它是有效的JSON,但我收到同样的错误。
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 :(得分:0)
你必须像下面那样增加计数变量。
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 ++;
}
错误就在这里
/ n {&#34; result1&#34;:[{&#34; pid&#34;:&#34; 55708&#34;,&#34; pname&#34;}的字符0的预期字面值:&#34;约翰逊&#34;&#34;单价&#34;:&#34; 42&#34;},{&#34; PID&#34;:&#34; 16592&#34;&# 34; pname&#34;:&#34; Hamam Soap&#34;,&#34; unitprice&#34;:&#34; 30&#34;},{&#34; pid&#34;:&#34; 02931&#34;,&#34; pname&#34;:&#34; Santoor Soap&#34;,&#34; unitprice&#34;:&#34; 26&#34;},{&#34; pid&# 34;:&#34; 71036&#34;,&#34; pname&#34;:&#34; Vivel Soap&#34;,&#34; unitprice&#34;:&#34; 50&#34;}, {&#34; PID&#34;:&#34; 25942&#34;&#34; PNAME&#34;:&#34;勒克斯&#34;&#34;单价&#34;:&#34; 28&#34;},{&#34; PID&#34;:&#34; 22658&#34;&#34; PNAME&#34;:&#34;勒克斯&#34;&#34;单价&#34 ;:&#34; 28&#34;},{&#34; PID&#34;:&#34; 66156&#34;&#34; PNAME&#34;:&#34;橄榄&#34;,& #34;单价&#34;:&#34; 50&#34;},{&#34; PID&#34;:&#34; 16004&#34;&#34; PNAME&#34;:&#34; Navaratna Oil&#34;,&#34; unitprice&#34;:&#34; 65&#34;},{&#34; pid&#34;:&#34; 44752&#34;,&#34; pname&# 34;:&#34;乐事&#34;&#34;单价&#34;:&#34; 20&#34;},{&#34; PID&#34;:&#34; 62542&#34 ;, &#34; PNA我&#34;:&#34;迈索尔桑德尔&#34;,&#34; unitprice&#34;:&#34; 70&#34;},{&#34; pid&#34;:&#34; 78276&# 34;,&#34; pname&#34;:&#34; Chik Shampoo&#34;,&#34; unitprice&#34;:&#34; 3&#34;},{&#34; pid&#34; :&#34; 12629&#34;,&#34; pname&#34;:&#34; Head&amp;肩&#34;&#34;单价&#34;:&#34; 3&#34;},{&#34; PID&#34;:&#34; 23524&#34;&#34; PNAME&#34 ;:&#34; ThumsUp&#34;&#34;单价&#34;:&#34; 20&#34;},{&#34; PID&#34;:&#34; 38026&#34;,& #34; pname&#34;:&#34; Dettol Soap&#34;,&#34; unitprice&#34;:&#34; 30&#34;}]} / n
答案 1 :(得分:0)
这对我来说很好用:
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);
}
但是,要小心你目前正在做的事情。您的while
循环遍历整个result1数组。因此,您的变量pid
Product_name
product_Price
将具有数组最后一个条目的值。