#/usr/bin/python2
#encoding=utf-8  
import sys
import urllib
import urllib2
import json
import ssl
from datetime import datetime,timedelta

sys.path.append(r'C:\Users\admin\Documents\Visual Studio 2015\Projects\PythonApplication1\PythonApplication1')  
from ObjectModel import ObjectModel

class Tool:

   #公开函数：将返回的JSON字符串解析成多个ObjectModel的集合
    def get_target_value(self,key, dic, tmp_list):
        """
        :param key: 目标key值
        :param dic: JSON数据
        :param tmp_list: 用于存储获取的数据
        :return: list
        """
        if not isinstance(dic, dict) or not isinstance(tmp_list, list):  # 对传入数据进行格式校验
            return 'argv[1] not an dict or argv[-1] not an list '
        if key in dic.keys():
            tmp_list.append(dic[key])  # 传入数据存在则存入tmp_list
        else:
            for value in dic.values():  # 传入数据不符合则对其value值进行遍历
                if isinstance(value, dict):
                    self.get_target_value(key, value, tmp_list)  # 传入数据的value值是字典，则直接调用自身
                elif isinstance(value, (list, tuple)):
                    self._get_value(key, value, tmp_list)  # 传入数据的value值是列表或者元组，则调用_get_value
        return tmp_list



    #下划线：私有，外部类就别调用了
    def _get_value(self,key, val, tmp_list):
        for val_ in val:
            #k = type(val_)
            if isinstance(val_, dict):  
                self.get_target_value(key, val_, tmp_list)  # 传入数据的value值是字典，则调用get_target_value
            elif isinstance(val_, (list, tuple)):
                self._get_value(key, val_, tmp_list)   # 传入数据的value值是列表或者元组，则调用自身