0

Hye. I've this json string returned by a php script (2 different mysql queries on 2 different tables): (sorry, that's a french application)

[{
  "statut":"2",
  "0":"2",
  "lib_statut":"En cours de traitement",
  "1":"En cours de traitement",
  "appelant":"GUEDIDER Jacqueline",
  "2":"GUEDIDER Jacqueline",
  "nom":"Virginie SORREL",
  "3":"Virginie SORREL",
  "date_creation":"05\/04\/2012 \u00e0 14:14:55",
  "4":"05\/04\/2012 \u00e0 14:14:55",
  "titre":"FACEO - Plus de message d'attente",
  "5":"FACEO - Plus de message d'attente"
},
{
  "nom":"Jonathan MORET",
  "0":"Jonathan MORET",
  "date_inter":"06\/04\/2012 \u00e0 11:18:07",
  "1":"06\/04\/2012 \u00e0 11:18:07"
},
{
  "nom":"Jonathan MORET",
  "0":"Jonathan MORET",
  "date_inter":"20\/04\/2012 \u00e0 08:22:53",
  "1":"20\/04\/2012 \u00e0 08:22:53"
}]

First Question: Does anybody knows why each row is recorded twice (first with an index, second with column name) ?

Second: How to parse both objects in my jquery script ?

Thanks in advance.

1
  • Can we see the queries that build it? That probably has to do with why everything is twice. Commented Jun 6, 2012 at 21:10

3 Answers 3

1

The reason everything appears twice, I confidently predict, is because the person who created that JSON did so by using the direct output of mysql_fetch_array() with no $result_type argument. This would also be true if using mysqli_fetch_array()/mysqli_result::fetch_array().

Tell them (you?) to use mysql_fetch_assoc() instead.

In fact, use PDO instead.

If you do this, you don't need to worry about processing "both" objects because you will only have one.

Sign up to request clarification or add additional context in comments.

Comments

0

For the first point, maybe because you use mysql_fetch_array (http://ca.php.net/manual/en/function.mysql-fetch-array.php) without the second parameter (MYSQL_ASSOC, MYSQL_BOTH, MYSQL_NUM)

Default is MYSQL_BOTH, and the function return an associative array with numeric key AND with the select param.

And for the second point, you can use

$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback
});

(http://api.jquery.com/jQuery.getJSON/)

Comments

0

First Question: Does anybody knows why each row is recorded twice (first with an index, second with column name) ?

Maybe a wrong SQL Query ... ;) seriuously, without the table structure and the query it's not possible to say why ..;

Second: How to parse both objects in my jquery script ?

i assume that you have your JSON assigned to a Javacript var datas : here is how you can access your datas :

var datas = "yOURJSONSTRING"; // Replace here your json ...
// then
var firstEl = datas[0];
// Then you can use : 
console.log("Statut is : " +firstEl.statut); // consle is for debugging purpose on FF
// then for 2nd and 3rd elements: 
var sndEl = datas[1];
var trdtEl = datas[2];

Hope this will help ...

Regards;

mimiz

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.