dqm4675 2013-05-29 14:12
浏览 75
已采纳

JSON单值数组

I am sending information from an Android tablet to SQL Server. I am doing this with a web service made in PHP, sending the information in the tablet in JSON format. It works fine, but I have a problem. See, one of the elements in the JSON is an array itself. And that array may have multiple entries or it may have only one entry. I'm not sure if you can understand what I'm saying, but here's an example:

<plant>
  <inspection>
    <problem>
      <id></id>
      <category></category>
      <description></description>
    </problem>
    <problem>
      <id></id>
      <category></category>
      <description></description>
    </problem>
  </inspection>
</plant>

In this example above, the code works fine. As you can see, in this example the inspection has two problems. It works fine if it's two or more problems. However, when it is only ONE problem, the JSON doesn't write it as an array and I have problems reading it over in my Web Service. Here is the Java code I use to convert the data to JSON:

                JSONObject holder = new JSONObject();
                JSONObject plant;
                JSONObject problem;

                DatabaseHandler handler = new DatabaseHandler(getApplicationContext());
                ArrayList<Plant> plants = handler.GetAllPlants();
                Inspection inspection;
                ArrayList<Problem> problems;

                if (plants != null) {
                    for (int i = 0; i < plants.size(); i++) {
                        plant = new JSONObject();
//                      handler.SetSynced(plants.get(i));
                        plant.put("plantID", plants.get(i).getPlantID());
                        plant.put("dome", plants.get(i).getDome());
                        plant.put("potholder", plants.get(i).getPotHolder());
                        plant.put("pot", plants.get(i).getPot());

                        inspection = handler.GetInspectionSync(plants.get(i));
                        if (inspection != null) {
                            plant.put("inspectionID", inspection.getInspectionID());
                            plant.put("inspectionDate", inspection.getInspectionDate());
                        }

                        problems = handler.GetAllProblems(inspection);
                        for (int k = 0; k < problems.size(); k++) {
                            problem = new JSONObject();
                            problem.put("problemID", problems.get(k).getProblemID());
                            problem.put("categoryID", problems.get(k).getCategoryID());
                            problem.put("problem", problems.get(k).getProblem());
                            problem.put("nokernel", problems.get(k).getNoKernel());
                            plant.accumulate("problems", problem);
                        }
                        holder.accumulate("plants", plant);
                    }

It is sloppy I know. The program was supposed to work in a whole different way but at the last minute the users changed everything and I had to improvise with what I had to make it on time. This is the PHP code:

if($conn) {
    foreach ($input['plants'] as $plant) {
        $query = 'INSERT INTO plants VALUES(' . $plant['plantID'] . ', ' . $plant['dome'] . ', ' .
                    $plant['potholder'] . ', ' . $plant['pot'] . ');';
        $params = array(1, "some data");
        $result = sqlsrv_query($conn, $query, $params);
        $query = 'INSERT INTO inspections VALUES(' . $plant['inspectionID'] . ', ' . $plant['plantID'] . ', \'' .
                    $plant['inspectionDate'] . '\');';
        $params = array(1, "some data");
        sqlsrv_query($conn, $query, $params);
        foreach($plant['problems'] as $problem) {
            $queryP = 'INSERT INTO problems VALUES(' . $problem['problemID'] . ', ' . $problem['categoryID'] . ', ' .
                        $plant['inspectionID'] . ', \'' . $problem['problem'] . '\', \'' . $problem['nokernel'] . '\');';
            fwrite($f, $queryP);
            $params = array(1, "some data");
            sqlsrv_query($conn, $queryP, $params);
        }
    }

Any help will be appreciated. This is the only thing I need to fix now to finally finish this proyect.

  • 写回答

1条回答 默认 最新

  • douwojiao5919 2013-05-29 14:20
    关注

    Maybe before the:

    foreach($plant['problems'] as $problem) {
    

    You could add this:

    if (is_assoc($plant['problems'])) {
       $plant['problems'] = array($plant['problems']);
    }
    

    And this somewhere else: is_assoc function

    function is_assoc($array) {
      return (bool)count(array_filter(array_keys($array), 'is_string'));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?