0

I am trying to manipulate a JSON file with PHP and am encountering the problem, that the function

$json = json_encode($cd, JSON_UNESCAPED_UNICODE);

is not working properly.

Code:

    <?php 
    $contents = file_get_contents('file.json');
    $ut=  utf8_encode($contents);
    $cd = json_decode($ut, true);        
    $cd['File'][0]['Name']="AnotherName"; 
    $json = json_encode($cd, JSON_UNESCAPED_UNICODE);
    file_put_contents('general.json', $json);
    ?>

The manipulating is working, however there are some ä,ö,ü and / symbols, which are changed to ä... and / in the output file. Is there any way to fix that?

Thanks

0

1 Answer 1

1

Assuming your file.json contains actual valid JSON encoded in UTF-8, the only issue you have is that you're messing up your encoding by using utf8_encode on it. utf8_encode converts from ISO-8859-1 to UTF-8, which is unnecessary since your input is not ISO-8859-1. The problem has nothing to do with the json_encode function.

Consider reading What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text.

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

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.