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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看