Solved

I have some json I need to decode

Posted on 2015-02-18
9
82 Views
Last Modified: 2015-02-19
I have some json I need to decode, alter and then encode without messing up any characters.

If I have a unicode character in a json string it will not decode.

so my code in php creates json in correct fromat except for one thing...

in my database i have "Sergio Pérez"

and it is comming through json as null    
"surname":null,"Construtor":"Force India

Open in new window

0
Comment
Question by:James Murrell
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 40617682
JSON encoding/decoding should work fine with unicode. Are you certain the problem is not occurring before the encoding happens? Are you maybe trying to perform single-byte character operations like str_replace() on values that could contain multi-byte characters?
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 40617683
Please provide a full example, showing the sample value, your script that accepts the value, and the script that encodes/decodes.
0
 
LVL 31

Author Comment

by:James Murrell
ID: 40617700
code in in php

include("members_area/db.php");
//$sql = sprintf("select * from races where raceID = '%s';", $_POST['F1']);
$sql = sprintf("SELECT d.surname, con.name AS Construtor, res.positionOrder FROM  races AS ra INNER JOIN results AS res ON ra.raceId = res.raceId INNER JOIN  seasons AS s ON ra.year = s.year INNER JOIN  drivers AS d ON res.driverId = d.driverId   INNER JOIN constructors AS con ON res.constructorId = con.constructorId  GROUP BY s.year , ra.round , ra.name , ra.date , d.surname , con.name , res.points ,ra.raceID, res.positionOrder having ra.raceID = '%s' ORDER BY ra.round DESC , res.positionOrder ;", $_POST['F1']);

$result = mysql_query($sql);
//var_dump( mysql_fetch_array($result) );
$raceData = array();
while ($raceResult = mysql_fetch_array($result, MYSQL_ASSOC)) {
   $raceData['results'][] = $raceResult;
}
echo json_encode( $raceData );
//var_dump( $raceData );

Open in new window


all these come out of database as
Hülkenberg
Räikkönen
Gutiérrez
Pérez

but in json it says null

full json is

{"results":[{"surname":"Hamilton","Construtor":"Mercedes","positionOrder":"1"},{"surname":"Rosberg","Construtor":"Mercedes","positionOrder":"2"},{"surname":"Vettel","Construtor":"Red Bull","positionOrder":"3"},{"surname":"Alonso","Construtor":"Ferrari","positionOrder":"4"},{"surname":null,"Construtor":"Force India","positionOrder":"5"},{"surname":"Button","Construtor":"McLaren","positionOrder":"6"},{"surname":"Massa","Construtor":"Williams","positionOrder":"7"},{"surname":"Bottas","Construtor":"Williams","positionOrder":"8"},{"surname":"Magnussen","Construtor":"McLaren","positionOrder":"9"},{"surname":"Kvyat","Construtor":"Toro Rosso","positionOrder":"10"},{"surname":"Grosjean","Construtor":"Lotus F1","positionOrder":"11"},{"surname":null,"Construtor":"Ferrari","positionOrder":"12"},{"surname":"Kobayashi","Construtor":"Caterham","positionOrder":"13"},{"surname":"Ericsson","Construtor":"Caterham","positionOrder":"14"},{"surname":"Chilton","Construtor":"Marussia","positionOrder":"15"},{"surname":"Ricciardo","Construtor":"Red Bull","positionOrder":"16"},{"surname":null,"Construtor":"Sauber","positionOrder":"17"},{"surname":"Sutil","Construtor":"Sauber","positionOrder":"18"},{"surname":"Vergne","Construtor":"Toro Rosso","positionOrder":"19"},{"surname":"Bianchi","Construtor":"Marussia","positionOrder":"20"},{"surname":"Maldonado","Construtor":"Lotus F1","positionOrder":"21"},{"surname":null,"Construtor":"Force India","positionOrder":"22"}]}

Open in new window

0
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 40617823
Try utf8_encode()-ing the values first:

while ($raceResult = mysql_fetch_array($result, MYSQL_ASSOC)) {
   $raceResult["surname"] = utf8_encode($raceResult["surname"]);
   $raceData['results'][] = $raceResult;
}
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 31

Author Closing Comment

by:James Murrell
ID: 40617833
thanks your way worked:


plus i tried mysql_query("SET NAMES 'utf8'"); and that worked to thanks
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40617842
This article may be helpful in understanding the character encoding issues:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11880-Unicode-PHP-and-Character-Collisions.html

JSON requires valid UTF-8, and that's the direction the world is taking.  You might want to consider converting the database, too.
http://iconoun.com/articles/collisions/Unicode_Presentation.pdf
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40617845
Just a thought, you might want to check out the difference between mysql_query("SET NAMES 'utf8'"); and mysql_query("SET NAMES 'utf8mb4'");
0
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 40617856
Hey James,

Only just got back to the PC, and you've already solved this one. Basically the reason you were having problems is because at least one of your layers is not set up for UTF-8 encoding. As you've figured out - it's the database. When you create tables in mySQL, set them to UTF-8 encoding at that stage and it will avoid this issue.

Basically, by calling SET NAMES you're saying to mySQL - give me the data back in UTF-8 encoding no matter how it's stored, but you'll need to do this with every query you run. Gonzo's solution performs the same task, just after the data has come back (now I've got the data, UTF encode it). Again, something you'll have to do each time you retrieve data from the database.

To avoid this, you need to make sure each layer is set to UTF-8 right from the start. The HTML page template I showed you earlier was set to UTF-8, although the page you now have online is back to iso-8859-1. Try to make sure all your layers are set to the same, and if you're planning on using non-latin characters, UTF-8 is the way to go.

If you want to change your database (and your should), run the following queries:

ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;

I would also suggest you change the encoding of your HTML page back to UTF-8
0
 
LVL 31

Author Comment

by:James Murrell
ID: 40618386
Hi thanks for the extra help

1. changing the format back to UTF-8 made no difference to problem but will be changing it back later.
2. the database is not mine and t supplied by  Ergast Developer API  http://ergast.com/mrd/ so i cannot change
3. utf8mb4 i will look this up later today (thanks)

Once again Thanks
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now