开发者

Porting a Python JSON API to Java (GWT)

开发者 https://www.devze.com 2023-04-05 05:47 出处:网络
I am trying to port this bit of Python code to Java http://www.bemasher.net/archives/1002 (scroll down to the bottom for the code)

I am trying to port this bit of Python code to Java http://www.bemasher.net/archives/1002 (scroll down to the bottom for the code)

But because everything is dynamically typed, I'm having difficulty porting this. I need to be able to use a Java implementation of Pythons json. Right now I'm using gson ( http://code.google.com/p/google-gson/ ), but I'm open to anything.

Neither of gson's fromJson's signatures

public <T> T fromJson(String json, Class<T> classOfT) throws JsonParseException
public <T> T fromJson(String json, Type typeOfT) throws JsonSyntaxException 

match Pythons

data = json.loads(response)

This is the JSON that I am trying to read: http://www.ows.newegg.com/Stores.egg/Categories开发者_运维问答/1

How would I go about getting the class or type of the JSON above and read through it? All the other examples I've seen you have to know what it is.

Thanks!


It basically fetches http://www.ows.newegg.com/Stores.egg/Menus and dumps its contents to a string (response), then it parses the JSON using json.loads() and puts that into data.

Then it will iterate through the contents of data and build a HTML list using lxml. The list itself should look like:

<ul>
  <li><a href="/Store?{StoreID}">{StoreTitle}</a></li>
  ...
</ul>

Where {StoreID} and {StoreTitle} are the IDs and titles fetched and parsed from that URL.

References: urllib2, json, lxml


I forgot to mention that I was using Google Web Toolkit - sorry about that. I went ahead and accepted @NullUserException 's answer though I'm writing this for anyone else who may come across this.

When converting this from Python to the Java (GWT) equivalant, I found that there was already support in GWT for JSON, and even better it works on the client side saving trips to the server. (source: JSON GWT API)

To keep thoe code clean I am using a wrapper for it that can be found on Google Code here.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号