Array within an object, how is the correct syntax?

Asked

Viewed 53 times

2

I am creating a personal account manager. I need to do this with localstorage. Ai am trying to assemble an object with all the debts of a category (in this ex. the category is Bradesco).

{
"nameCategory": "Bradesco",
"arrayDebits": {
    ["GVT": {
        "value": "220,00",
        "PaymentDate": "10/10/2010"
    }, "Agua": {
        "value": "150,00",
        "PaymentDate": "15/15/2015"
    }]
}

Each debit needs to be an object, I thought of putting the name of the object as the name of the account (GVT phone, water bill, electricity and etc.) and the value of the account and the due date as elements within that object. But this syntax is wrong according to the site: jsonlint giving this error:

Error: Parse error on line 3: ... "arrayDebits": ! ["GVT": ! "value" -------------------------------------------------------- Expecting 'STRING', '}', got '['

I’m missing where?

1 answer

2


You’re using wrong syntax, with [] inside {}, what you want is the other way around, since arrayDebits is an array with objects inside:

[{obj1}, {obj2}, etc...]

that would look like this:

{
  "nameCategory": "Bradesco",
  "arrayDebits": [
    {
      "GVT": {
        "value": "220,00",
        "PaymentDate": "10/10/2010"
      },
      "Agua": {
        "value": "150,00",
        "PaymentDate": "15/15/2015"
      }
    }
  ]
}
  • Vlw, more because I need the keys after [ and before closing the ]?

  • @Carlosm [ ] that is to say array. In the example above you have an array, with 1 object inside. I assume that later you will have N right objects, or is it just 1? If it’s only 1 you don’t need to [ ] you can take and leave only the object.

  • I want each input of the array to be a different object... It can be like this? { "nameCategory": "Bradesco", "arrayDebits": [{ "GVT": { "value": "220,00", "PaymentDate": "10/10/2010" } }, { "Agua": { "value": "150,00", "PaymentDate": "15/15/2015" } } ] }

  • 1

    @Carlosm yes, it can. I thought that GVT and Agua would be the same, but if they are not like that it works. But in this case, I would do something like this: in view of having Agua and GVT outside the internal object would: {"nameCategory":"Bradesco","arrayDebits":[{"type":"GVT","value":"220,00","PaymentDate":"10/10/2010"},{"type":"Agua","value":"150,00","PaymentDate":"15/15/2015"}]}

  • @Carlosm like this is much easier to use after

  • 1

    show, I’ll use it like this... vlw!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.