【问题标题】:How do i add these JSONS我如何添加这些 JSONS
【发布时间】:2015-11-16 13:27:54
【问题描述】:

我正在尝试合并一些 json。我试过these answers,但我认为它们不符合我的需求。

我有 2 个(或更多)这些 jsons

{
    item: {
        icon: "http://services.runescape.com/m=itemdb_rs/4922_obj_sprite.gif?id=2",
        icon_large: "http://services.runescape.com/m=itemdb_rs/4922_obj_big.gif?id=2",
        id: 2,
        type: "Default",
        typeIcon: "http://www.runescape.com/img/categories/Default",
        name: "Cannonball",
        description: "Ammo for the Dwarf Cannon.",
        current: {
            trend: "neutral",
            price: 208
        },
        today: {
            trend: "positive",
            price: "+8"
        },
        members: "true",
        day30: {
            trend: "positive",
            change: "+8.0%"
        },
        day90: {
            trend: "negative",
            change: "-4.0%"
        },
        day180: {
            trend: "positive",
            change: "+9.0%"
        }
    }
}

如何将它们添加到这样的东西中?

{
    items: [
        {
            icon: "http://services.runescape.com/m=itemdb_rs/4922_obj_sprite.gif?id=2",
            icon_large: "http://services.runescape.com/m=itemdb_rs/4922_obj_big.gif?id=2",
            id: 2,
            type: "Default",
            typeIcon: "http://www.runescape.com/img/categories/Default",
            name: "Cannonball",
            description: "Ammo for the Dwarf Cannon.",
            //Same other stuff here
        },
        {
            icon: "http://services.runescape.com/m=itemdb_rs/4922_obj_sprite.gif?id=2",
            icon_large: "http://services.runescape.com/m=itemdb_rs/4922_obj_big.gif?id=2",
            id: 2,
            type: "Default",
            typeIcon: "http://www.runescape.com/img/categories/Default",
            name: "Cannonball",
            description: "Ammo for the Dwarf Cannon.",
            //Same other stuff here     
        }
    ]
}

【问题讨论】:

  • @AlexAndrei 我尝试了链接中的答案和一些接近他们的答案。第一个给了我NULL,第二个给了我尝试合并的最后一个json。
  • 不知何故错过了你的第一句话,抱歉。您能否发布所有 json 字符串,或者我可以复制第一个字符串吗?
  • 生成的 json 应该是什么样子
  • @dlporter98 喜欢底部的json
  • 请不要将答案编辑到您的问题中。选择对您有帮助的答案并将其标记为已接受,或发布您自己的答案并接受。

标签: php json


【解决方案1】:

我不知道每个 json 结构的来源,但我认为这将是可以迭代的东西:

$finalArray = array();
foreach ($jsonStructs as $json)
{
    $j = json_decode($json, true);
    $finalArray['items'][] = $j['item'];
}

$finalJson = json_encode( $finalArray);

我将每个 json 结构解码为一个 php 数组。然后我从中获取项目值并将其放入另一个数组中,该数组将包含每个值。

最后,我将 finally 数组编码为 json,它将为您提供所需的内容。

【讨论】:

    【解决方案2】:

    首先JSON开头的字符串无效,键没有用双引号括起来,所以我们需要解决这个问题。

    使用初始字符串重复 10 次的示例代码

    <?php
    
    $string = file_get_contents('sample.json');
    
    //var_dump($string);
    
    $data = jsonDecode($string);
    
    for ($i=0;$i<=10;$i++)
        $list['items'][] = $data['item'];
    
    print json_encode($list);
    
    function jsonDecode($string, $assoc=true, $fixNames=true){
      if(strpos($string, '(') === 0){
        $string = substr($string, 1, strlen($string) - 2); // remove outer ( and )
      }
      if($fixNames){
        $string = preg_replace("/(?<!\"|'|\w)([a-zA-Z0-9_]+?)(?!\"|'|\w)\s?:/", "\"$1\":", $string);
      }
      return json_decode($string, $assoc);
    }
    

    json 修复函数取自这里https://stackoverflow.com/a/17748840/5043552
    输出符合要求

    【讨论】:

    • 我相信这些密钥在我解码时是经过编码的。那我可以跳过你的解码部分吗?
    • 当然,如果不需要修复,请将其删除并构建列表。您可以轻松检查 JSON 响应并做出决定
    猜你喜欢
    • 2016-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 2017-05-06
    • 1970-01-01
    相关资源
    最近更新 更多