我有一个PHP文件,它使我的数据库中的某些数据生成JSONArray
。我在我的localhost(使用简单的PHP)上有这个,它运行良好。
在我将它转移到真正的主机后,我收到一个错误:
Org.json.jsonexception:Value ... java.lang.String类型的脚本 无法转换为JSONObject
我的json看起来像是:
{
"chemData": [{
"ID": "1",
"TYPE": "AD_SOLVENTBASE",
"COMPANY": "MORCHEM",
"SOLIDC": "65.000",
"GRAMNEED": "3.200",
"SOLUSE": "0.450",
"PRICE": "15000"
}, {
"ID": "2",
"TYPE": "AD_SOLVENTLESS",
"COMPANY": "MORCHEM",
"SOLIDC": "100.000",
"GRAMNEED": "2.300",
"SOLUSE": "0.000",
"PRICE": "15000"
}, {
"ID": "3",
"TYPE": "HEATSEAL_PS",
"COMPANY": "BUJAN",
"SOLIDC": "25.000",
"GRAMNEED": "5.000",
"SOLUSE": "0.250",
"PRICE": "26000"
}, {
"ID": "4",
"TYPE": "COLDSEAL",
"COMPANY": "AZARAN_BASPAR",
"SOLIDC": "50.000",
"GRAMNEED": "3.000",
"SOLUSE": "0.000",
"PRICE": "14000"
}, {
"ID": "5",
"TYPE": "INK",
"COMPANY": "BEHROFARAN",
"SOLIDC": "37.000",
"GRAMNEED": "2.000",
"SOLUSE": "0.450",
"PRICE": "12500"
}, {
"ID": "6",
"TYPE": "EASYPEEL",
"COMPANY": "BUJAN",
"SOLIDC": "25.000",
"GRAMNEED": "5.000",
"SOLUSE": "0.250",
"PRICE": "50000"
}],
"filmData": [{
"id": "1",
"Film Type": "PE",
"Density": "0.9200",
"Price": "6000"
}, {
"id": "2",
"Film Type": "CPP",
"Density": "0.9200",
"Price": "9500"
}, {
"id": "3",
"Film Type": "PET",
"Density": "1.4100",
"Price": "12000"
}, {
"id": "4",
"Film Type": "BOPP",
"Density": "0.9300",
"Price": "9500"
}, {
"id": "5",
"Film Type": "Al",
"Density": "2.7400",
"Price": "12000"
}, {
"id": "6",
"Film Type": "BOPP MET",
"Density": "0.9300",
"Price": "10000"
}],
"cost": [{
"ID": "1",
"WORKERPART": "720",
"ENERGYPART": "80",
"BANKNUZUL": "1700",
"TRANSFERIN": "100",
"TRANSFERTO": "0",
"PACKING": "60",
"WASTE": "4",
"COMMISION": "80",
"SOLVENTPRICE": "4300"
}]
}
答案 0 :(得分:0)
在这里,试试这段代码:
try {
JSONObject rootObj = new JSONObject(yourJson);
JSONArray chemData = rootObj.getJSONArray("chemData");
JSONArray filmData = rootObj.getJSONArray("filmData");
JSONArray cost = rootObj.getJSONArray("cost");
for (int i = 0; i < chemData.length(); i++) {
JSONObject obj = chemData.getJSONObject(i);
String id = obj.getString("ID");
obj.getString("TYPE");
String company = obj.getString("COMPANY");
obj.getString("SOLIDC");
obj.getString("GRAMNEED");
obj.getString("SOLUSE");
obj.getString("PRICE");
Log.d("testing", id + " " + company);
}
for (int i = 0; i < filmData.length(); i++) {
JSONObject obj = filmData.getJSONObject(i);
String id = obj.getString("id");
obj.getString("Film Type");
obj.getString("Density");
String price = obj.getString("Price");
Log.d("testing", id + " " + price);
}
for (int i = 0; i < cost.length(); i++) {
JSONObject obj = cost.getJSONObject(i);
String id = obj.getString("ID");
obj.getString("WORKERPART");
obj.getString("ENERGYPART");
obj.getString("BANKNUZUL");
obj.getString("TRANSFERIN");
obj.getString("TRANSFERTO");
obj.getString("PACKING");
String waste = obj.getString("WASTE");
obj.getString("COMMISION");
obj.getString("SOLVENTPRICE");
Log.d("testing", id + " " + waste);
}
} catch (JSONException ex) {
ex.printStackTrace();
}
你应该创建三个pojo类来将你的数据存储在 yourPojo 类型的ArrayList中。
希望这有帮助。
答案 1 :(得分:-1)
这是开始时的解析部分:
public void onResponse(JSONObject response) {
JSONArray [] jsonArrays = new JSONArray[response.length()];
try {
jsonArrays [0] = response.getJSONArray("chemData");
jsonArrays [1] = response.getJSONArray("filmData");
jsonArrays [2] = response.getJSONArray("cost");
}catch (JSONException e) {
Toast.makeText(SecondActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}