3

I am receiving the following JSON as a result of a web callout:

{
  "count" : 1,
  "posts" : {
    "1234567" : {
      "id" : "1234567",
      "private" : false,
      "message" : "0D5j0000004bV8oCAE - Salesforce Chatter - New post on Monday Jan 5th",
      "subject_ref" : null,
      "created_at" : "2015-01-05T09:56:59-08:00",
      "story_id" : null,
      "newest_reply_at" : null,
      "reply" : false,
      "subject_type" : null,
      "subject_id" : null,
      "user_id" : "09876543",
      "updated_at" : "2015-01-05T09:56:59-08:00",
      "workspace_id" : "4569872",
      "has_attachments" : false,
      "reply_count" : 0,
      "formatted_message" : "<p>0D5j0000004bV8oCAE - Salesforce Chatter - New post on Monday Jan 5th</p>"
    }
  },
  "results" : [
    {
      "id" : "1234567",
      "key" : "posts"
    }
  ]
}

And I am making use of JSON.deserializeUntyped as follows:

Map<String,Object> m = (Map<String,Object>) JSON.deserializeUntyped(res.getBody());
Map<String,Object> posts = (Map<String,Object>)m.get('posts');

List<String> return_posts = new List<String>();

for (String p : posts.keySet()) {
    List<String>  pp = (List<String>)posts.get(p);
    return_posts.add(pp);
}

However I am running into a couple of issues:

  1. "Incompatible element type LIST for collection of String when trying to add the posts array to return_posts inside the loop.
  2. If I comment out the line causing error #1 above, the code compiles but throws this error at runtime:

    FATAL_ERROR System.TypeException: Invalid conversion from runtime type MAP to LIST

My goal is to create a list/array with jus the post content, similar to this:

{
    "id" : "1234567",
    "private" : false,
    "message" : "message body"
    etc....
},
{
    "id" : "0000000",
    "private" : false,
    "message" : "some more message body"
    etc....
},

There appears to be some type mismatching going on, and I may be missing one loop level to get to the "posts" level, but I'm having difficulty getting to the root of the issue. Any help would be really appreciated. Thanks!

6

The problem is that you're casting to a list when the object is a map. You want a list of maps. This should do it:

List<Map<String,Object>> return_posts = new List<Map<String,Object>>();

for (String p : posts.keySet()) {
    Map<String,Object>  pp = (Map<String,Object>)posts.get(p);
    return_posts.add(pp);
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.