Note that there are some explanatory texts on larger screens.

plurals
  1. POJSON single value arrays
    primarykey
    data
    text
    <p>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:</p> <pre><code>&lt;plant&gt; &lt;inspection&gt; &lt;problem&gt; &lt;id&gt;&lt;/id&gt; &lt;category&gt;&lt;/category&gt; &lt;description&gt;&lt;/description&gt; &lt;/problem&gt; &lt;problem&gt; &lt;id&gt;&lt;/id&gt; &lt;category&gt;&lt;/category&gt; &lt;description&gt;&lt;/description&gt; &lt;/problem&gt; &lt;/inspection&gt; &lt;/plant&gt; </code></pre> <p>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:</p> <pre><code> JSONObject holder = new JSONObject(); JSONObject plant; JSONObject problem; DatabaseHandler handler = new DatabaseHandler(getApplicationContext()); ArrayList&lt;Plant&gt; plants = handler.GetAllPlants(); Inspection inspection; ArrayList&lt;Problem&gt; problems; if (plants != null) { for (int i = 0; i &lt; 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 &lt; 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); } </code></pre> <p>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:</p> <pre><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); } } </code></pre> <p>Any help will be appreciated. This is the only thing I need to fix now to finally finish this proyect.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload