dryl34156
2013-01-07 11:05
浏览 18
已采纳

不使用密钥创建PHP对象?

I've got a request to present the data in the following format as a JSON feed:

{
    "id": "123",
    "info": {
        "code": "ZGE",
        "description": "test1",
        "type": "AVL",
        "date": "09/08/2012"
    }
},
{
   "id": "456",
    "info": {
        "code": "ZDN",
        "description": "test2",
        "type": "CLR",
        "date": "16/02/2012"
    }
}

However in my PHP code, I think I need to have a key itterator - but I end up with this format:

{

"0": {
    "id": "123",
    "info": {
        "code": "ZGE",
        "description": "test1",
        "type": "AVL",
        "date": "09/08/2012"
    }
},
"1": {

    "id": "456",
    "info": {
        "code": "ZDN",
        "description": "test2",
        "type": "CLR",
        "date": "16/02/2012"
    }
}
}

Any ideas on how to build the first data set with out having the index iterator?

图片转代码服务由CSDN问答提供 功能建议

我已经请求以下列格式显示数据作为JSON提要: \ n

  {
“id”:“123”,
“info”:{
“code”:“ZGE”,
“description”:“test1”,
“ 键入“:”AVL“,
”日期“:”09/08/2012“
} 
},
 {
”id“:”456“,
”info“:{
” 代码“:”ZDN“,
”description“:”test2“,
”type“:”CLR“,
”date“:”16/02/2012“
} 
} 
   
 
 

但是在我的PHP代码中,我认为我需要一个密钥itterator - 但我最终得到这种格式:

  {
 
“0”:{
“id”:“123”,
“info”:{
“code”:“ZGE”,
“description”:“test1”,
  “type”:“AVL”,
“date”:“09/08/2012”
} 
},
“1”:{
 
“id”:“456”,
“  info“:{
”code“:”ZDN“,
”description“:”test2“,
”type“:”CLR“,
”date“:”16/02/2012“
}  
} 
} 
   
 
 

有关如何构建第一个数据集的任何想法 有索引迭代器吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • doushuangdui5419 2013-01-07 11:23
    已采纳

    It can be built like this:

    $arr = array(
        array(
            'id' => 123,
            'info' => array(
                'code' => 'ZGE',
                'description' => 'test1',
                'type' => 'AVL'
            )
        ),
        array(
            'id' => 456,
            'info' => array(
                'code' => 'ZDN',
                'description' => 'test2',
                'type' => 'CLR'
            )
        )
    );
    
    echo json_encode($arr);
    

    Outputs

    [
        {
            "id": 123,
            "info": {
                "code": "ZGE",
                "description": "test1",
                "type": "AVL"
            }
        },
        {
            "id": 456,
            "info": {
                "code": "ZDN",
                "description": "test2",
                "type": "CLR"
            }
        }
    ]
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongyao2129 2013-01-07 11:08

    simple create an array of objects, no need for the key (notice the [ ] surrounding your list)

    json.txt

    [{
        "id": "123",
        "info": {
            "code": "ZGE",
            "description": "test1",
            "type": "AVL",
            "date": "09/08/2012"
        }
    },
    {
       "id": "456",
        "info": {
            "code": "ZDN",
            "description": "test2",
            "type": "CLR",
            "date": "16/02/2012"
        }
    }]
    

    example.php

    <?php
        $data = json_decode(file_get_contents('./json.txt'));
    ?>
    
    评论
    解决 无用
    打赏 举报
  • duanlu9816 2013-01-07 11:20

    The only reason json_encode should produce the output you're seeing is adding another named key to the array that you're passing to json_encode, by default it should work as you want:

    $json = '[
        {
            "id": "123",
            "recall_info": {
                "code":"ZGE",
                "description": "test1",
                "type": "AVL",
                "date": "09/08/2012"
            }
        },
        {
            "id": "123",
            "recall_info": {
                "code": "ZDN",
                "description": "test2",
                "type": "CLR",
                "date": "16/02/2012"
            }
        }
    ]';
    
    $php = array(
        (object) array(
            'id' => '123',
            'recall_info' => (object) array(
                'code' => 'ZGE',
                'description' => 'test1',
                'type' => 'AVL',
                'date' => '09/08/2012'
            )
        ),
        (object) array(
            'id' => '123',
            'recall_info' => (object) array(
                'code' => 'ZGE',
                'description' => 'test2',
                'type' => 'CLR',
                'date' => '16/02/2012'
            )
        )
    );
    
    var_dump(json_encode($php));
    
    评论
    解决 无用
    打赏 举报
  • duanhoupeng6642 2013-01-07 11:21

    the JSON format you've specified in the first example (ie the requested format) is not valid JSON.

    A valid JSON string must evaluate to a single Javascript object; the example you've given evaluates to two Javascript objects, separated by a comma. In order to make it valid, you would need to either enclose the whole thing in square brackets, to turn it into a JS array or enclose it in curly braces, and give each of the two objects a key.

    The PHP code you've written is doing the second of these two options. It is therefore generating valid JSON code, about as close to the original request as could be expected while still being valid.

    It would help if you'd shown us the PHP code that you've used to do this; without that, I can't really give you advice on how to improve it, but if you want to switch to the square bracket notation, all you need is to put your PHP objects into an unkeyed array, and json_encode() should do it all for you; you shouldn't need to use a keyed array or an iterator for that.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题