我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用json.py()。
def add(conf, var, val): if var not in conf: conf[var] = val return if isinstance(val, dict) and isinstance(conf[var], dict): conf[var].update(val) return if not isinstance(conf[var], list): conf[var] = [conf[var]] if isinstance(val, list): conf[var] += val else: conf[var].append(val) # Scan to end of JSON object. Grrr, why can't json.py do this without # reading all of fp?
def main(): global logger logger = logging.getLogger('compare-viewer-json') if (len(sys.argv) != 3): sys.stderr.write("**** ERROR: Wrong number of arguments.\n") sys.stderr.write("Usage: compare-viewer-json.py <file1> <file2> \n") sys.exit(1) # Read the 1st file and create a DNA structure object from it. file_name1 = sys.argv[1] dna_structure1 = DnaStructure(file_name1) dna_structure1.read_json(file_name1) logger.info("") # Read the 2nd file and create a DNA structure object from it. file_name2 = sys.argv[2] dna_structure2 = DnaStructure(file_name2) dna_structure2.read_json(file_name2) # Compare the data. if not dna_structure1.compare(dna_structure2): sys.exit(1)
def json_text(): """Returns the parser for Json formatted data""" # Taken from https://github.com/vlasovskikh/funcparserlib/blob/master/funcparserlib/tests/json.py # and modified slightly unwrap = lambda x: x.value null = (n('null') | n('Null')) >> const(None) >> unwrap value = forward_decl() member = (string >> unwrap) + op_(u':') + value >> tuple object = ( op_(u'{') + maybe(member + many(op_(u',') + member) + maybe(op_(','))) + op_(u'}') >> make_object) array = ( op_(u'[') + maybe(value + many(op_(u',') + value) + maybe(op_(','))) + op_(u']') >> make_array) value.define( null | (true >> unwrap) | (false >> unwrap) | object | array | (number >> unwrap) | (string >> unwrap)) json_text = object | array return json_text
def showInfo(): print "CSV to JSON Converter" print "USAGE: csv-to-json.py <CSVFILE>" print "" print "Use I/O redirection for creating resulting files. Example:" print "python ./csv-to-json.py sample.csv > sample.json"
def get_lexer(mime, explicit_json=False, body=''): # Build candidate mime type and lexer names. mime_types, lexer_names = [mime], [] type_, subtype = mime.split('/', 1) if '+' not in subtype: lexer_names.append(subtype) else: subtype_name, subtype_suffix = subtype.split('+', 1) lexer_names.extend([subtype_name, subtype_suffix]) mime_types.extend([ '%s/%s' % (type_, subtype_name), '%s/%s' % (type_, subtype_suffix) ]) # As a last resort, if no lexer feels responsible, and # the subtype contains 'json', take the JSON lexer if 'json' in subtype: lexer_names.append('json') # Try to resolve the right lexer. lexer = None for mime_type in mime_types: try: lexer = pygments.lexers.get_lexer_for_mimetype(mime_type) break except ClassNotFound: pass else: for name in lexer_names: try: lexer = pygments.lexers.get_lexer_by_name(name) except ClassNotFound: pass if explicit_json and body and (not lexer or isinstance(lexer, TextLexer)): # JSON response with an incorrect Content-Type? try: json.loads(body) # FIXME: the body also gets parsed in json.py except ValueError: pass # Nope else: lexer = pygments.lexers.get_lexer_by_name('json') return lexer