Form of a JSON [duplicate]

0

To explain my problem better, I have this JSON:

{
"Person": [
    {
        "Id":1, 
        "Type":1,
        "Extra":
            [{
                "Number":88888888,
                "Code":506
            }]
        ,
        "Personal":{
            "Name":"Mario"
        }   
    },
     {
        "Id":1, 
        "Type":1,
        "Extra":
            [{
                "Number":88888888,
                "Code":506
            }]
        ,
        "Personal":{
            "Name":"Mario"
        }   
    }
    ] 
}

I need a JSON to have several "Person" as something like this:

{
"Person": [
    {
        "Id":1, 
        "Type":1,
        "Extra":
            [{
                "Number":88888888,
                "Code":506
            }]
        ,
        "Personal":{
            "Name":"Mario"
        }   
    },
     {
        "Id":1, 
        "Type":1,
        "Extra":
            [{
                "Number":88888888,
                "Code":506
            }]
        ,
        "Personal":{
            "Name":"Mario"
        }   
    }
    ],

{
"Person": [
    {
        "Id":1, 
        "Type":1,
        "Extra":
            [{
                "Number":88888888,
                "Code":506
            }]
        ,
        "Personal":{
            "Name":"Mario"
        }   
    },
     {
        "Id":1, 
        "Type":1,
        "Extra":
            [{
                "Number":88888888,
                "Code":506
            }]
        ,
        "Personal":{
            "Name":"Mario"
        }   
    }
    ] 
}
}

But apparently it's not as simple as putting just a comma ... could someone please give me an example and explain how it would be done in those cases?

Thank you!

    
asked by java005 10.09.2018 в 16:11
source

3 answers

5

Your JSON is poorly structured, so you can see your object Person has an array of objects. in your way of trying to apply it, you add a Person within your Person root leaving the object something like this:

Person{
    [ {Objeto identificador},{Objeto identificador} ],
    Person { ... }
}

What you can do is an array of Person from the beginning of your JSON, leaving something like this (Note how the JSON starts as an array and the next Person is added when closing the first Person ):

[{
    "Person": [{
        "Id": 1,
        "Type": 1,
        "Extra": [{
            "Number": 88888888,
            "Code": 506
        }],
        "Personal": {
            "Name": "Mario"
        }
    },
    {
        "Id": 1,
        "Type": 1,
        "Extra": [{
            "Number": 88888888,
            "Code": 506
        }],
        "Personal": {
            "Name": "Mario"
        }
    }]
},
{
    "Person": [{
        "Id": 1,
        "Type": 1,
        "Extra": [{
            "Number": 88888888,
            "Code": 506
        }],
        "Personal": {
            "Name": "Mario"
        }
    },
    {
        "Id": 1,
        "Type": 1,
        "Extra": [{
            "Number": 88888888,
            "Code": 506
        }],
        "Personal": {
            "Name": "Mario"
        }
    }]
}]
    
answered by 10.09.2018 / 16:21
source
1

I explain first how the JSON works.

Within the JSON data exchange format there are two main structures: lists / arrays, and objects.

The lists or arrangements are composed of different objects. And the objects store the pairs composed by key / value to define information within them.

Now yes. Taking into account the above information, if you want to define different objects of type person what you have to do is define all those people within a list / arrangement. That way you can access each person through their index (this is how they handle the lists / arrangements, the index starts from the number 0).

How would the structure look? Well, this will change according to the information you want to manage, but taking into account the structures that you put as an example, it would be more appropriate to consider structures with the following form:

[
    {
        "Id":1, 
        "Type":1,
        "Extra":
            [{
                "Number":88888888,
                "Code":506
            }]
        ,
        "Personal":{
            "Name":"Mario"
        }   
    },
    {
        "Id":2, 
        "Type":1,
        "Extra":
            [{
                "Number":88888888,
                "Code":506
            }]
        ,
        "Personal":{
            "Name": "Otro Mario"
        }   
    }
]

Notice how within the list / array [] all objects are placed {} , which for this specific case represent people.

Finally, if you really want to assign a name to the array, you can declare the JSON structure as follows:

{
  "Persons": [
    {
      "Extra": [
        {
          "Code": 506, 
          "Number": 88888888
        }
      ], 
      "Id": 1, 
      "Personal": {
        "Name": "Mario"
      }, 
      "Type": 1
    }, 
    {
      "Extra": [
        {
          "Code": 506, 
          "Number": 88888888
        }
      ], 
      "Id": 1, 
      "Personal": {
        "Name": "Mario"
      }, 
      "Type": 1
    }
  ]
}
    
answered by 10.09.2018 в 16:37
0

I imagine what you want is something like this:

{
    "Persons": [{
        "Person": {
            "Id": 1,
            "Type": 1,
            "Extra": [{
                "Number": 88888888,
                "Code": 506
            }],
            "Personal": {
                "Name": "Mario"
            }
        }
    }, {
        "Person": {
            "Id": 2,
            "Type": 2,
            "Extra": [{
                "Number": 88888888,
                "Code": 506
            }],
            "Personal": {
                "Name": "Carla"
            }
        }
    }]
}

You can test your JSON on this page

    
answered by 10.09.2018 в 16:19