解决方法:无法解析 Json 数组。 "Not a primitive array"错误



我无法解析 json 数组。当我尝试时,我得到一个"不是原始数组错误。 你能帮我找到我的错误吗?

我一直在寻找解决方案,但找不到。

这是我的代码:

public void tableInit() throws JSONException {
Log.e("TableInit()", "Start!");
Log.e("TableInit()", ""+jsonObjectToTable);
float textSize = 18.0f;
TableLayout stk = (TableLayout) findViewById(R.id.table_main);
TableRow tbrow0 = new TableRow(this);
TextView tv0 = new TextView(this);
tv0.setText(" ID ");
tv0.setTypeface(null, Typeface.BOLD); //fett
tv0.setTextSize(textSize);
tbrow0.addView(tv0);
[...]
stk.addView(tbrow0);

JSONArray jsonArray = new JSONArray(jsonObjectToTable);
for(int i = 0; i < jsonArray.length(); ++i)
{
// Extract values from JSON row:
JSONObject jsonObject      = jsonArray.getJSONObject(i);
Log.e("JsonObject", ""+jsonObject);
String     id     = jsonObject.has("id")     ? jsonObject.getString("id") : "";
String     name    = jsonObject.has("name")    ? jsonObject.getString("name") : "";
String     home = jsonObject.has("home") ? jsonObject.getString("home") : "";
String     wetsuit     = jsonObject.has("wetsuit")     ? jsonObject.getString("wetsuit") : "";
String     board     = jsonObject.has("board")     ? jsonObject.getString("board") : "";
String     rig     = jsonObject.has("rig")     ? jsonObject.getString("rig") : "";
String     harness     = jsonObject.has("harness")     ? jsonObject.getString("harness") : "";
String     rent_until     = jsonObject.has("rent_until")     ? jsonObject.getString("rent_until") : "";
String     renting_date     = jsonObject.has("renting_date")     ? jsonObject.getString("renting_date") : "";
TableRow tbrow = new TableRow(this);
TextView textViewId = new TextView(this);
textViewId.setText(id);
textViewId.setGravity(Gravity.CENTER);
tbrow.addView(textViewId);
[...]
}
}

这是创建的 jsonObjectToTable:

@Override
protected String doInBackground(String... strings) {
Log.e("Async", "Async starts!");
try {
response = RequestHandler.sendGet("http://192.168.122.1/php/renting_private_select.php");
return response;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected void onPostExecute(String s) {
try {
JSONObject tableJson = new JSONObject(s);
jsonObjectToTable = tableJson;
tableInit();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}

我收到以下日志:

E/TableInit((: 开始! E/TableInit((: {"equipment_private": [{"id":"1","name":"Name","home":"Unterkunft","潜水服":"Anzug","board":"Board","rig":"Segel","Harness":"Trapetz","renting_date":"23.01.19","rent_until":"23.12.19"},{"id":"2","name":"Name","home":"Unterkunft","潜水服":"Anzug","board":"Board","rig","rig":"Segel","Harness":"Trapetz2","renting_date":","rent_until":"}]} W/System.err: org.json.JSONException: Not a primitive array: class org.json.JSONObject W/System.err: at org.json.JSONArray.(JSONArray.java:116( at com.example.surftest.MainActivity.tableInit(MainActivity.java:135( W/System.err: at com.example.surftest.MainActivity$RequestSelectAsync.onPostExecute(MainActivity.java:234( at com.example.surftest.MainActivity$RequestSelectAsync.onPostExecute(MainActivity.java:213( at android.os.AsyncTask.finish(AsyncTask.java:695( at android.os.AsyncTask.access$600(AsyncTask.java:180( at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712( W/System.err: at android.os.Handler.dispatchMessage(Handler.java:106( at android.os.Looper.loop(Looper.java:193( at android.app.ActivityThread.main(ActivityThread.java:6669( at java.lang.reflect.Method.invoke(Native Method( W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493( at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858(

您做错的是,您正在创建 json 数组的对象并传递 json 对象本身,而在您的 json 响应中,json 数组嵌入到与键"equipment_private"相关的 json 对象中。
错误的方式:
JSONArray jsonArray = new JSONArray(jsonObjectToTable);
正确的方式:
JSONArray jsonArray = jsonObjectToTable.getJSONArray("equipment_private")
希望你能理解,这将解决你的问题。

这是因为你得到的表本身就是一个对象。看看这个就知道了。您只需要多一行即可完成代码:

这是您的输入

{
"equipment_private": [{
"id": "1",
"name": "Name",
"home": "Unterkunft",
"wetsuit": "Anzug",
"board": "Board",
"rig": "Segel",
"harness": "Trapetz",
"renting_date": "23.01.19",
"rent_until": "23.12.19"
}, {
"id": "2",
"name": "Name",
"home": "Unterkunft",
"wetsuit": "Anzug",
"board": "Board",
"rig": "Segel",
"harness": "Trapetz2",
"renting_date": "",
"rent_until": ""
}]
}

所以你应该解析如下:

JSONObject object = new JSONObject(jsonObjectToTable);  // You do need this line
try {
object.get("equipment_private");
JSONArray jsonArray = new JSONArray(object);
for (int i = 0; i < jsonArray.length(); ++i) {
// Extract values from JSON row:
JSONObject jsonObject = jsonArray.getJSONObject(i);
Log.e("JsonObject", "" + jsonObject);
String id = jsonObject.has("id") ? jsonObject.getString("id") : "";
String name = jsonObject.has("name") ? jsonObject.getString("name") : "";
String home = jsonObject.has("home") ? jsonObject.getString("home") : "";
String wetsuit = jsonObject.has("wetsuit") ? jsonObject.getString("wetsuit") : "";
String board = jsonObject.has("board") ? jsonObject.getString("board") : "";
String rig = jsonObject.has("rig") ? jsonObject.getString("rig") : "";
String harness = jsonObject.has("harness") ? jsonObject.getString("harness") : "";
String rent_until = jsonObject.has("rent_until") ? jsonObject.getString("rent_until") : "";
String renting_date = jsonObject.has("renting_date") ? jsonObject.getString("renting_date") : "";
TableRow tbrow = new TableRow(this);
TextView textViewId = new TextView(this);
textViewId.setText(id);
textViewId.setGravity(Gravity.CENTER);
tbrow.addView(textViewId);
[...]
}
} catch (JSONException e) {
e.printStackTrace();
}

现在,如果您遇到任何错误,请告诉我

此外,您可以通过创建表示 Json 对象的类来解决问题,然后使用 GSON 转换器对其进行解析,而无需编写任何这些代码。 您可以在此线程中找到示例解决方案 如何解析 JSON 解析 在安卓中使用 GSON

您的整体解析方式不太正确。

出于此类目的,我会建议像 GSON 这样的东西。

要使用它,首先创建两个模型类。

class Response{
@SerializedName("equipment_private")
public List<Equipment> equipmentPrivate
} 
class Equipment {
public String id;
public String name;
public String home;
public String wetsuit;
public String board;
public String rig;
public String harness;
@SerializedName("renting_date")
public String rentingDate;
@SerializedName("rent_until")
public String rentUntil;
}

之后你可以做一些类似的事情

Gson gson = new Gson();
Response response = gson.fromJson(json_string, Response.class);

也许它需要一些额外的工作,因为我没有在 IDE 中检查它,但我想你会掌握的。希望它会有所帮助。

最新更新