我在android中获取双jsonarray来提取而不是获得结果

时间:2016-11-29 10:02:04

标签: android json

My result image of android app

这是我得到的输出图像我需要获取名称的值。我正在尝试的代码是

JSONArray jsonArray = new JSONArray(result);

JSONObject jsonObject = new JSONObject(jsonArray);

 String name = jsonObject.getString("name");

但它不起作用。任何人都可以帮我这个 非常感谢

这是我正在处理的后端php代码

$aircraftId = filter_input(INPUT_POST, 'aircraftId');
            $arr = array();
            $stmt = $db->prepare("SELECT * FROM temp WHERE aircraft_id = :aid");
            $stmt->execute(array(':aid' => $aircraftId));
            $data = $stmt->fetchAll(PDO::FETCH_OBJ);
            $x = 0;
            foreach ($data as $in)
            {
                $arr[$x][] = array(
                'id' =>  $data->id,
                'name'  => $data->name,
                'label'  => $data->label,
                'description'  => $data->description,
                'type'  => $data->type,
                'value'  => $data->value,
                'deliminator'  => $data->deliminator,
                'deliminator2'  => $data->deliminator2,
                'title'  => $data->title,
                'part_number'  => $data->part_number,
                'serial_number'  => $data->serial_number,
                'mm'  =>$data->mm,
                'cost' =>$data->cost,
                'category' =>$data->category,
                'timeInstall' => $data->timeInstall,
                'customType' => $data->customType,
                'engineNumber' => $data->engineNumber,
                'activateMargin' => $data->activateMargin,
                'margin0' => $data->margin0,
                'margin1' => $data->margin1,
                'margin2' => $data->margin2,
                'margin3' => $data->margin3,
                'displayField' => $data->displayField,
                'tso1' => $data->tso1,
                'tso2' => $data->tso2,
                'tso3' => $data->tso3,
                'tso4'  => $data->tso4,
                'interval1' =>  $data->interval1,
                'interval2' => $data->interval2,
                'interval3' => $data->interval3,
                'interval4' => $data->interval4,
                'rmg1' => $data->rgm1,
                'rmg2' => $data->rmg2,
                'rmg3' => $data->rmg3,
                'rmg4' => $data->rmg4,
                'lcw1' => $data->lcw1,
                'lcw2' => $data->lcw2,
                'lcw3' => $data->lcw3,
                'lcw4' =>  $data->lcw4
            );
                $x = $x+1;
            }

            echo json_encode($arr);

3 个答案:

答案 0 :(得分:1)

您没有正确解析响应。 记住" ["显示JSONArray已启动,并且在响应中你有两个[这意味着你需要获得两个JSONArrays才能在JSONObject中获取JSONObject和eventualy数据。

试试这个

try {
        JSONArray responseArray = new JSONArray(result);
        JSONArray innerArray = responseArray.getJSONArray(0);
        for (int i = 0; i < innerArray.length(); i++) {
            JSONObject jsonObject = innerArray.getJSONObject(i);
            String id = jsonObject.getString("id");
            String name = jsonObject.getString("name");
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
如果你在第一个数组中获得多个数组,那么@ Tabish的代码会更好。

答案 1 :(得分:0)

您的第一行是正确的WorksheetFunction.CountIf(ThisWorkbook.Worksheets(1).UsedRange, "*" & TextBox1.Value & "*")

下一个语句应该是for循环。迭代jsonArray对象。例如:

JSONArray jsonArray = new JSONArray(result);

你能试试吗?

答案 2 :(得分:0)

有两个JsonArrays然后JsonObject出现了

JsonArray jsonArray = new JsonArray(results);
for (int i  = 0 ; i< jsonArray.length() ; i++){
    JsonArray jsonArray1 = jsonArray.get(i);
      for(int j = 0 ; j < jsonArray1.length() ; j++){
          JsonObject jsonObject = jsonArray1.get(j);
          String name  = jsonObject.getAsString("name");
      }
}