dongyizhui0616 2012-05-15 08:17
浏览 56
已采纳

在php中加载大量的json数据

I'm a beginner with this so sorry for the silly question,

I have a json with a large amount of data in it and I have to process this file in php. (Edit/Delete/Filter Objects).

How I am thinking to do it: a class that contains a list of objects and the functions to load objects from json and another to write them back. Also, other functions for operations over the list.

What I'm asking is what is the best way to do this, because my idea is the simplest one, than each time I click on : Edit, Delete, Filter I have to reload again and again the loatJsonObjects(), can you give me a better way to do this, that won't make so many operations each time? Thank you !

  • 写回答

3条回答 默认 最新

  • dreamworld2007 2012-05-15 08:28
    关注

    So, I shouldn't really attempt to write one for you, but I'm in a really good mood this morning. Try this:

    Class Json_Handler {
      private $json_object;
      public function __construct($json_data) {
    
        if(is_object($json_data) {
          $this->json_object = $json_data;
        }
    
      }
    
      public function edit($key, $value) {
    
        if(isset($this->json_data[$key])) {
         $this->json_data[$key] = $value;
         return $this;
        }
        else {
         return FALSE;
        }
    
      }
    
      public function delete($key) {
        if(isset($this->json_data[$key])) {
          unset($this->json_data[$key]);
          return $this;
        }
        else {
          return FALSE;
        }
      }
    
      public function filter($on = 'key', $filter == NULL) {
         $filtered_objects = array();
         switch($on) {
           case 'key':
           default:
            foreach($this->json_data as $key => $value) {
              if($key == $value) {
                $filtered_objects[] = array($key => $value);
              }
            }
            break;
            case 'value':
             foreach($this->json_data as $key => $value) {
               $filtered_objects[] = array($key => $value);
             }
            break;
         }
         return $filtered_objects;
      }
    
      public function get_json() {
        return json_encode($this->json_data);
      }
    
      public function set_json($json) {
        $this->json_data = (is_object($json) ? $json : json_encode($json));
        return $this;
      }
    
    }
    

    You can then use it as so:

    $my_json_data = json_decode([...]);
    $json = new Json_Handler($my_json_data);
    echo $json->edit('test', 'now i have this value')
              ->delete('test')
              ->filter('key', 'bar');
              ->set_json(json_encode($new_json)
              ->get_json();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题