0

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.

Esoteric Screen Name
  • 6,082
  • 4
  • 29
  • 38
plasmy
  • 99
  • 3
  • 9
  • 32
  • 2
    this is talking about json but sample is showing xml. – Orangepill May 29 '13 at 14:13
  • just do a count on the contents of the inspection tag if its got >1 sub sections then treat as an array / multi element if its got one or less treat as a single element. – Dave May 29 '13 at 14:15
  • 1
    JSON is something like `{plant:{inspection : {problems:[{id:12, category:15,description:'This is a demo'},{...},{...}...]}}}}` – JoDev May 29 '13 at 14:16

1 Answers1

1

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'));
}
Community
  • 1
  • 1
Josejulio
  • 1,286
  • 1
  • 16
  • 30