3

Well I've been trying for like 3 hours now. Using lots of apis it still doesn't work.

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 to an array/list. Any help?

7
  • Is that one entry in the list? Commented Oct 28, 2011 at 19:35
  • 1
    What APIs or libraries have you been using? Why doesn't it work? What have you tried? Commented Oct 28, 2011 at 19:36
  • gson, flexjson and a lot more. I'm a bit of a newbie when it comes to java (More of a php person <.<) Commented Oct 28, 2011 at 19:37
  • This is valid JSON according to python's json module. Do you mean that it won't parse, or that the contents don't meet your requirements? Commented Oct 28, 2011 at 19:38
  • Well with gson it just said it couldn't deser' it. So yea, I kinda have no idea what's wrong. Commented Oct 28, 2011 at 19:39

2 Answers 2

6

I'm using Jackson from http://codehaus.org/ and so far it has lived up to all my needs.

You don't quite deal with json as raw strings in an arraylist, but rather as POJOs, here's a quick example with a subset of your 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 with the values filled in. Then you can use that object for anything you need.

Here are a couple of links for the Jackson project:

http://jackson.codehaus.org/

http://wiki.fasterxml.com/JacksonInFiveMinutes

The latest all in one JAR is here:

http://jackson.codehaus.org/1.9.1/jackson-all-1.9.1.jar

Sign up to request clarification or add additional context in comments.

Comments

1

You should try JavaJson from source forge... you can parse that this way:

JsonObject json = JsonObject.parse("...");
/*
 * or also JsonObject.parse(inputStream);
 */
then you can get fields this way:
String title = json.getString("title");
String username = json.get("user", "username").toString();

and so on. here's the link: https://sourceforge.net/projects/javajson/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.