如何动态创建JSON对象?

时间:2022-11-25 08:19:09

I would like to create a JSON object dynamically. The JSON object will have to be as the follows:

我想动态创建一个JSON对象。 JSON对象必须如下所示:

{ 
                $capa:[$fila['test_name'],...etc],
                .
                .
                .etc    

};

The key and value will be retrieved through a MySQL query.
This is what i'm doing:

密钥和值将通过MySQL查询检索。这就是我正在做的事情:

$array_container= array();

while($fila=mysqli_fetch_assoc($sql)){


                    $format_org=str_replace(" ","_",$fila["organization"]);
                    $format_eval=str_replace(" ","_",$fila["EvaluationType"]);
                    $format_test=str_replace(" ","_",$fila["test_name"]);
                                        $CapaEnviar=$format_org.$format_eval;

                              $array_container[] = array($CapaEnviar => $fila['test_name']);

}

echo json_encode($array_container,true);

Using the previous code, I can retrieve a JSON object with duplicate keys.

使用前面的代码,我可以检索具有重复键的JSON对象。

This code is an answer for an AJAX request, so once the JSON object has been created correctly, I will send back this JSON object in order to retrieve the key and value, so I will have to retrieve separately the key and value.

此代码是AJAX请求的答案,因此一旦正确创建了JSON对象,我将发送回此JSON对象以检索键和值,因此我将必须单独检索键和值。

1 个解决方案

#1


2  

From your comment,

从你的评论,

...the results of this array is as the follows: [{"TEST1":"valueT1"},{"TEST1":"otherValue"}] and what i'm looking for is to have a json like this: [{"TEST1":['valueT1','otherValue']}] as you can see, i want to avoid duplicates keys.

...这个数组的结果如下:[{“TEST1”:“valueT1”},{“TEST1”:“otherValue”}]我正在寻找的是这样的json:[ {“TEST1”:['valueT1','otherValue']}]如您所见,我想避免重复键。

Solution:

解:

In your while loop, change this line

在while循环中,更改此行

$array_container[] = array($CapaEnviar => $fila['test_name']);

to

$array_container[$CapaEnviar][] = $fila['test_name'];

Update:

更新:

how i can retrieve this key and their values through ajax?

我如何通过ajax检索此密钥及其值?

Since you're expecting a json object from server, add this setting dataType:'json' to your AJAX request. dataType is the type of data you're expecting back from the server. And in the success() callback function, loop through the json result to get (key, value) pairs

由于您期望从服务器获取json对象,因此将此设置dataType:'json'添加到您的AJAX请求中。 dataType是您期望从服务器返回的数据类型。并且在success()回调函数中,循环遍历json结果以获取(key,value)对

Here's the reference:

这是参考:

So your AJAX skeleton code should be like this:

所以你的AJAX框架代码应该是这样的:

$.ajax({
    type: 'POST',
    url: 'yourpage.php',
    dataType: 'json',
    cache: 'false',

    beforeSend: function(){

    },

    success: function(data){
        $.each(data, function(key, value) {
            alert("Key:" + key + ", value: " + value);
        });
    },

    error: function(){
        // error
    }
});

#1


2  

From your comment,

从你的评论,

...the results of this array is as the follows: [{"TEST1":"valueT1"},{"TEST1":"otherValue"}] and what i'm looking for is to have a json like this: [{"TEST1":['valueT1','otherValue']}] as you can see, i want to avoid duplicates keys.

...这个数组的结果如下:[{“TEST1”:“valueT1”},{“TEST1”:“otherValue”}]我正在寻找的是这样的json:[ {“TEST1”:['valueT1','otherValue']}]如您所见,我想避免重复键。

Solution:

解:

In your while loop, change this line

在while循环中,更改此行

$array_container[] = array($CapaEnviar => $fila['test_name']);

to

$array_container[$CapaEnviar][] = $fila['test_name'];

Update:

更新:

how i can retrieve this key and their values through ajax?

我如何通过ajax检索此密钥及其值?

Since you're expecting a json object from server, add this setting dataType:'json' to your AJAX request. dataType is the type of data you're expecting back from the server. And in the success() callback function, loop through the json result to get (key, value) pairs

由于您期望从服务器获取json对象,因此将此设置dataType:'json'添加到您的AJAX请求中。 dataType是您期望从服务器返回的数据类型。并且在success()回调函数中,循环遍历json结果以获取(key,value)对

Here's the reference:

这是参考:

So your AJAX skeleton code should be like this:

所以你的AJAX框架代码应该是这样的:

$.ajax({
    type: 'POST',
    url: 'yourpage.php',
    dataType: 'json',
    cache: 'false',

    beforeSend: function(){

    },

    success: function(data){
        $.each(data, function(key, value) {
            alert("Key:" + key + ", value: " + value);
        });
    },

    error: function(){
        // error
    }
});