如何使用PHP替换我的JSON中的键内容并保存到Shopware 6.5?

huangapple go评论61阅读模式
英文:

how do I replace the content of a key in my json and save back on shopware 6.5 using php

问题

以下是您要翻译的部分:

我正在处理一个Shopware项目,需要设置一个包含所有已处理订单的JSON。

JSON存储在一个目录中,我已经取出并传递给了变量**$fil**。

$filer = $order1->getOrderNumber() . '_' . $country2->getIso() . '.json';
$fil = __DIR__ . '/' . $filer;

$file_contents = file_get_contents($fil);

然后将存储在变量**$fil中的JSON传递给变量$jsonData**。

$jsonData = $fil;

我正在尝试通过将JSON解码为关联数组来替换键的值,如下所示:

$data = json_decode($jsonData, true);

要替换的键如下所示(尝试替换ID):

$data['entity']['payments'][0]['state']['id'] = $logMessageId;

然后,我尝试将其保存回modifiedJsonData变量,并替换回目录中:

$modifiedJsonData = json_encode($data, JSON_PRETTY_PRINT);

file_put_contents($fil, $modifiedJsonData);

似乎它部分工作了,但没有完全成功,而是替换了整个JSON内容,而不仅仅是(ID)部分。

如下所示:$data['entity']['payments'][0]['state']['id']

它替换了JSON的所有内容,而JSON内容应该比下面的内容更多:

{
    "entity": {
        "payments": [
            {
                "state": {
                    "id": #####
                }
            }
        ]
    }
}

请帮忙,我可能做错了什么。

英文:

I am working on a shopware project that requires me setup a json of all orders that is processed.

the json is stored in a directory which I have gotten out and passed to a variable $fil.

$filer =  $order1->getOrderNumber() . '_' . $country2->getIso() . '.json';
$fil = __DIR__ . '/' . $filer;

$file_contents = file_get_contents($fil);

The gotten json stored in variable $fil is then passed to variable $jsonData

$jsonData = $fil;

I am trying to replace the value of a key as below by decoding the JSON into an associative array

$data = json_decode($jsonData, true);

The key to be replaced is as below (trying to replace ID)

$data['entity']['payments'][0]['state']['id'] = $logMessageId;

I then try to save it back in variable modifiedJsonData and replace back to directory

$modifiedJsonData = json_encode($data, JSON_PRETTY_PRINT);

file_put_contents($fil, $modifiedJsonData);

it seemed to work partially but not fully, instead of replacing the (ID) alone

as here : $data['entity']['payments'][0]['state']['id']

its replaces all the content of the json as below, the json content is supposed to be more than the below:

{
    "entity": {
        "payments": [
            {
                "state": {
                    "id": #####
                }
            }
        ]
    }
}

Please help, what could I be doing wrong

答案1

得分: 0

  1. JSON_THROW_ON_ERROR
  2. JSON_BIGINT_AS_STRING

Example:

$data = json_decode($jsonData, true, flags: JSON_THROW_ON_ERROR | JSON_BIGINT_AS_STRING);

(since PHP 8.0)

$data = json_decode($jsonData, true, 512, JSON_THROW_ON_ERROR | JSON_BIGINT_AS_STRING);

(since PHP 7.3)

JSON_THROW_ON_ERROR

The reason why you only see the JSON for $data['entity']['payments'][0]['state']['id'] is, because the $data = json_decode(...) operation returned null because the parsing failed. Using the JSON_THROW_ON_ERROR will turn any error into an exception, making this more visible because it stops the script on error.

This prevents you to search the error in later output.

Throws JsonException if an error occurs instead of setting the global error state that is retrieved with json_last_error() and json_last_error_msg(). JSON_PARTIAL_OUTPUT_ON_ERROR takes precedence over JSON_THROW_ON_ERROR. Available as of PHP 7.3.0. (PHP Manual)

JSON_BIGINT_AS_STRING

Decodes large integers as their original string value. (PHP Manual)

BIGINT is referring to BigInt (ECMA 262), a Javascript number value type for very large (negative and positive) integer numbers, json_decode() can easily loose precision with (3v4l.org demo):

var_dump(json_decode('4599999999999999999999999999999999'));
var_dump(json_decode('4599999999999999999999999999999999', true, 512, JSON_BIGINT_AS_STRING));
float(4.6E+33)
string(34) "4599999999999999999999999999999999"

英文:

Have the correct JSON Text in $jsonData and use two flags when you decode your JSON with json_decode(), the first always, the second more specifically in your current scenario:

  1. JSON_THROW_ON_ERROR
  2. JSON_BIGINT_AS_STRING

Example:

$data = json_decode($jsonData, true, flags: JSON_THROW_ON_ERROR | JSON_BIGINT_AS_STRING);

(since PHP 8.0)

$data = json_decode($jsonData, true, 512, JSON_THROW_ON_ERROR | JSON_BIGINT_AS_STRING);

(since PHP 7.3)

JSON_THROW_ON_ERROR

The reason why you only see the JSON for $data['entity']['payments'][0]['state']['id'] is, because the $data = json_decode(...) operation returned null because the parsing failed. Using the JSON_THROW_ON_ERROR will turn any error into an exception, making this more visible because it stops the script on error.

This prevents you to search the error in later output.

> Throws JsonException if an error occurs instead of setting the global error state that is retrieved with json_last_error() and json_last_error_msg(). JSON_PARTIAL_OUTPUT_ON_ERROR takes precedence over JSON_THROW_ON_ERROR. Available as of PHP 7.3.0. (PHP Manual)

JSON_BIGINT_AS_STRING

> Decodes large integers as their original string value. (PHP Manual)

BIGINT is referring to BigInt (ECMA 262), a Javascript number value type for very large (negative and positive) integer numbers, json_decode() can easily loose precision with (3v4l.org demo):

var_dump(json_decode('4599999999999999999999999999999999'));
var_dump(json_decode('4599999999999999999999999999999999', true, 512, JSON_BIGINT_AS_STRING));
float(4.6E+33)
string(34) "4599999999999999999999999999999999"

答案2

得分: 0

<?php
	$data = json_decode($jsonData, true, 512, JSON_BIGINT_AS_STRING);
	$data['entity']['payments'][0]['state']['id'] = $logMessageId;
    $modifiedJsonData = json_encode($data, JSON_PRETTY_PRINT);
	$existingData = json_decode(file_get_contents($fil), true);
	$existingData['entity']['payments'][0]['state']['id'] = $data['entity']['payments'][0]['state']['id'];
	$updatedJsonData = json_encode($existingData, JSON_PRETTY_PRINT);
	file_put_contents($fil, $updatedJsonData);
英文:

I was also able to find away around it by:

&lt;?php
	$data = json_decode($jsonData, true, 512, JSON_BIGINT_AS_STRING);
	$data[&#39;entity&#39;][&#39;payments&#39;][0][&#39;state&#39;][&#39;id&#39;] = $logMessageId;
    $modifiedJsonData = json_encode($data, JSON_PRETTY_PRINT);
	$existingData = json_decode(file_get_contents($fil), true);
	$existingData[&#39;entity&#39;][&#39;payments&#39;][0][&#39;state&#39;][&#39;id&#39;] = $data[&#39;entity&#39;][&#39;payments&#39;][0][&#39;state&#39;][&#39;id&#39;];
	$updatedJsonData = json_encode($existingData, JSON_PRETTY_PRINT);
	file_put_contents($fil, $updatedJsonData);

huangapple
  • 本文由 发表于 2023年7月23日 15:46:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747146.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定