0

I need to convert every element of 'content' array to object.

Here is json:

{
  "content": [
    {
      "id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
      "author": {
        "userId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
      },
      "createDate": "2020-01-30T20:18:29.764Z",
      "executor": {
        "userId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
      },
    }
  ],
  "first": true,
  "numberOfElements": 0,
}

The problem is that 'content' array is inside json, and its parts as executor and author has to be objects too, and I don't know how to reach it and parse. How it can be done? Any help, thanks.

1 Answer 1

1

You access the elements like this:

  var decoded = json.decode(j);
  var inner = decoded['content'][0]; // if you expect more than one entry, iterate the list
  print(inner['id']); // -> 3fa85f64-5717-4562-b3fc-2c963f66afa6
  print(inner['createDate']);
  print(inner['author'].runtimeType); // another Map<String, dynamic> as expected
  print(inner['author']['userId']);

You can create Dart classes to model, for example, a 'user' if you want.

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

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.