0

I want to ask about the JSON format. The results of Sql is

Year SumOfYear
2011 0.30
2012 0.19

update

this is the part of php code.

while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {

  $results[] = array(
     '"Year"' => $row['Year'],
     '"SumOfYear"' => $row['SumOfYear'],
     );

 }

echo json_encode($results);

The results is

[

{
Year: "2011",
SumOfYear: "0.30"
},

{
Year: "2012",
SumOfYear: "0.19"
}

]

if i want to have like below, is it still good format?

{
 2011,
 2012
},
{
 0.30,
 0.19
}

1 Answer 1

2

if the JSON code is the original, you have to put Year and SumOfYear between code signs:

[
   {
      "Year":"2011",
      "SumOfYear":"0.30"
   },
   {
      "Year":"2012",
      "SumOfYear":"0.19"
   }
]

If you want to verify your JSON, you can use this tool.

As you seem to use php, i recommend to structure your data in an array and let php encode it into json:

echo json_encode(my_data_array);
Sign up to request clarification or add additional context in comments.

3 Comments

thanks! that link is useful! what about second structure? is it wrong?
the second has the same syntax error: "Year" and "SumOfYear", and the syntax error, as you miss a comma between the object. But has an additional error: You can't have two objects with the same Key on the same layer. So there can only be one object with key year and the same for the key SumOfYear on the other layer. This will cause either an parsing error, or a loss of information.
Thanks. I updated it. can you tell second structure is good or bad? @lootsch

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.