simplejson

How to search/find In JSON with java

余生颓废 提交于 2019-11-27 23:10:16
I have a below JSON string from the below i want to find/search criteria in JSON String. 1). To find the Number of keys present. 2). To get the values of given key(if we have array) { "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of

SimpleJSON and NumPy array

可紊 提交于 2019-11-27 17:23:48
What is the most efficient way of serializing a numpy array using simplejson? I'd use simplejson.dumps(somearray.tolist()) as the most convenient approach (if I was still using simplejson at all, which implies being stuck with Python 2.5 or earlier; 2.6 and later have a standard library module json which works the same way, so of course I'd use that if the Python release in use supported it;-). In a quest for greater efficiency, you could subclass json.JSONEncoder (in json ; I don't know if the older simplejson already offered such customization possibilities) and, in the default method,

Python JSON encoding

北战南征 提交于 2019-11-27 09:50:14
问题 I'm trying to encode data to JSON in Python and I been having a quite a bit of trouble. I believe the problem is simply a misunderstanding. I'm relatively new to Python and never really got familiar with the various Python data types, so that's most likely what's messing me up. Currently I am declaring a list, looping through and another list, and appending one list within another: import simplejson, json data = [['apple', 'cat'], ['banana', 'dog'], ['pear', 'fish']] x = simplejson.loads(data

Getting values from JSON using Python

十年热恋 提交于 2019-11-27 07:45:30
While I am trying to retrieve values from JSON string, it gives me an error: data = json.loads('{"lat":444, "lon":555}') return data["lat"] But, if I iterate over the data, it gives me the elements ( lat and lon ), but not the values: data = json.loads('{"lat":444, "lon":555}') ret = '' for j in data: ret = ret + ' ' + j return ret Which returns: lat lon What do I need to do to get the values of lat and lon ? ( 444 and 555 ) If you want to iterate over both keys and values of the dictionary, do this: for key, value in data.items(): print key, value What error is it giving you? If you do

What is faster - Loading a pickled dictionary object or Loading a JSON file - to a dictionary? [closed]

你离开我真会死。 提交于 2019-11-27 07:42:45
What is faster: (A) 'Unpickling' (Loading) a pickled dictionary object, using pickle.load() or (B) Loading a JSON file to a dictionary using simplejson.load() Assuming: The pickled object file exists already in case A, and that the JSON file exists already in case B. alecxe The speed actually depends on the data, it's content and size. But, anyway, let's take an example json data and see what is faster (Ubuntu 12.04, python 2.7.3) : pickle cPickle json simplejson ujson yajl Giving this json structure dumped into test.json and test.pickle files: { "glossary": { "title": "example glossary",

Reading JSON from SimpleHTTPServer Post data

北城余情 提交于 2019-11-27 07:05:38
I am trying to build a simple REST server with python SimpleHTTPServer. I am having problem reading data from the post message. Please let me know if I am doing it right. from SimpleHTTPServer import SimpleHTTPRequestHandler import SocketServer import simplejson class S(SimpleHTTPRequestHandler): def _set_headers(self): self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() def do_GET(self): print "got get request %s" % (self.path) if self.path == '/': self.path = '/index.html' return SimpleHTTPRequestHandler.do_GET(self) def do_POST(self): print "got post!!"

SimpleJson handling of same named entities

倾然丶 夕夏残阳落幕 提交于 2019-11-27 04:07:19
问题 I'm using the Alchemy API in app engine so I'm using the simplejson library to parse responses. The problem is that the responses have entries that have the sme name { "status": "OK", "usage": "By accessing AlchemyAPI or using information generated by AlchemyAPI, you are agreeing to be bound by the AlchemyAPI Terms of Use: http://www.alchemyapi.com/company/terms.html", "url": "", "language": "english", "entities": [ { "type": "Person", "relevance": "0.33", "count": "1", "text": "Michael

Single versus double quotes in json loads in Python

随声附和 提交于 2019-11-27 02:50:11
问题 I notice that single quotes cause simplejson 's loads function to fail: >>> import simplejson as json >>> json.loads("\"foo\"") 'foo' >>> json.loads("\'foo\'") Traceback (most recent call last): ... ValueError: No JSON object could be decoded I'm parsing things like: foo = ["a", "b", "c"] from a textfile into lists in Python and would like to also accept foo = ['a', 'b', 'c'] . simplejson is convenient for making foo automatically into a list. How can I get loads to accept single quotes, or

jQuery.getJSON doesn't trigger callback

和自甴很熟 提交于 2019-11-27 01:31:29
问题 I have a html code: <button>asd</button> <script type = "text/javascript"> $('button').click( function() { $.getJSON('/schedule/test/', function(json) { alert('json: ' + json + ' ...'); }); } ); </script> and corresponding view: def test(request): if request.method == 'GET': json = simplejson.dumps('hello world!') return HttpResponse(json, mimetype = 'application/json') The view is executed (tested using print ), json variable is initialised but no alert appears. What did I do wrong? I've

Easiest way to serialize a simple class object with simplejson?

狂风中的少年 提交于 2019-11-27 00:10:13
问题 I'm trying to serialize a list of python objects with JSON (using simplejson) and am getting the error that the object "is not JSON serializable". The class is a simple class having fields that are only integers, strings, and floats, and inherits similar fields from one parent superclass, e.g.: class ParentClass: def __init__(self, foo): self.foo = foo class ChildClass(ParentClass): def __init__(self, foo, bar): ParentClass.__init__(self, foo) self.bar = bar bar1 = ChildClass(my_foo, my_bar)