Java JSON parsing
•
Java
Well, I've been trying for three hours now Using a lot of APIs, it still doesn't work properly
I'm trying to parse
{ "id": 8029390,"uid": "fdABNhroHsr0","user": { "username": "Skrillex","permalink": "skrillex" },"uri": "/skrillex/cat-rats","duration": 305042,"token": "VgA2a","name": "cat-rats","title": "CAT RATS","commentable": true,"revealComments": true,"commentUri": "/skrillex/cat-rats/comments/","streamUrl": "http://media.soundcloud.com/stream/fdABNhroHsr0?stream_token=VgA2a","waveformUrl": "http://w1.sndcdn.com/fdABNhroHsr0_m.png","propertiesUri": "/skrillex/cat-rats/properties/","statusUri": "/transcodings/fdABNhroHsr0","replacingUid": null,"preprocessingReady": null }
In an array / list Does it help?
Solution
I from http://codehaus.org/ I started using Jackson. So far, it has met all my needs
You don't think of JSON as the original string in the ArrayList, but as a POJO. Here is a quick example of a subset of JSON
public class JacksonExample { public static void main(String[] args) throws JsonParseException,JsonMappingException,IOException { String text = "{ \"id\": 8029390,\"user\": { \"username\": \"Skrillex\" } }"; ObjectMapper mapper = new ObjectMapper(); Pojo pojo = mapper.readValue(text,Pojo.class); System.out.println(pojo.id); System.out.println(pojo.user.username); } } class Pojo { public String id; public User user; public String getId() { return id; } public void setId(String id) { this.id = id; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public static class User { public String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } } }
The mapper creates a POJO object filled with values You can then use the object for anything you need
Here are some links to the Jackson project:
http://jackson.codehaus.org/
http://wiki.fasterxml.com/JacksonInFiveMinutes
The latest integrated jar is here:
http://jackson.codehaus.org/1.9.1/jackson-all-1.9.1.jar
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码