[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

I have some json I need to decode

Posted on 2015-02-18
9
Medium Priority
?
103 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 35

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 35

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 35

Accepted Solution

by:
gr8gonzo earned 2000 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;
}
1
 
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 111

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 111

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 44

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
1
 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

649 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