0

In C#, I fill a list

List<CareerOpportunities> jobs = new List<CareerOpportunities>();

with objects that hold job opportunities. I want convert "jobs" to JSON format like this:

    {
    "Job Title":{
    "Sex":"...",
    "City":"...",
    "Date":"...",
    "ActivityField":"...",
    "Salary":"...",
    "WorkHours":"...",
    "Insurance":"..."
    "Address":"..."
    },
    .
    .
    .
    .
    }

How can i do that?

I tried but i got this result:

[
{
"JobTitle":"...",
"Sex":"...",
"City":"...",
"Date":"...",
"ActivityField":"...",
"Salary":"... ",
"WorkHours":"...",
"Insurance":"...",
"Address":"...
},
.
.
.
.
]
Mafii
  • 6,538
  • 1
  • 33
  • 52
  • 1
    Please add more information on the structure of the data you're converting, and how you're converting to JSON. – Iucounu Oct 03 '16 at 07:25
  • 2
    The latter is a list and exactly what you seem to want. The former is invalid JSON. I'm sure you don't want invalid data as output. It's even semantially invalid since Job Title shouldn't have Salary os Sex in it. – Sami Kuhmonen Oct 03 '16 at 07:26
  • You got yourself an array of json objects, that sounds like exactly what you wanted. :) – Sir JokesALot Oct 03 '16 at 07:27
  • may it help you http://stackoverflow.com/questions/6201529/turn-c-sharp-object-into-a-json-string-in-net-4 – David Soler Oct 03 '16 at 07:39
  • try this. http://stackoverflow.com/questions/6201529/turn-c-sharp-object-into-a-json-string-in-net-4 – David Soler Oct 03 '16 at 07:41
  • Thank you Sami Kuhmonen, The first JSON format is object of key/values that values are objects too and that is correct format and i want this. the second part of your answer is true but if you consider this point of view that "Job Title" is what user sees and the rest is what user wants to see (for details), mine will be true as well. – Ali Eghbali Oct 03 '16 at 08:35
  • What does your `CareerOpportunities` .NET object look like? Include its definition. – Jamiec Oct 03 '16 at 08:53

2 Answers2

1

I think you should stick to the JSON serialization you got as result. This is what the consumer of a service, for example, expects to obtain. You have defined a list of items, and this should be serializated as an array of objects in JSON. If you want to obtains something like the code you wrote, you should define an object structure such as:

class CareerOportunities {
    public List<CareerOportunity> oportunities = new List<CareerOportunity>();
}

class CareerOportunity {
    public string JobTitle { get; set; }
    // more attributes here ...
}

This will be serialized as:

{
    "oportunities": [
        {
            "JobTitle": "..."
        },
        {
            "JobTitle": "..."
        }
        ...
    ]
}
Luis M. Villa
  • 107
  • 10
  • Thank you Luis. But i want a JSON format with one object that contains key/value that value is a object too and the key is job title. – Ali Eghbali Oct 03 '16 at 08:10
  • What I understand that you are trying to achieve is not possible. ¿Could you write a example ot the output you are expecting to obtain with dummy data? Two objects would be enought – Luis M. Villa Oct 03 '16 at 09:47
0

It is normal to have "[]" because the list is converted to an array, you cannot have a list without "[]"

If you just want "{}" around, just return a single object

Michel Amorosa
  • 357
  • 5
  • 8
  • Thank you Michel. If i want the first format (not a array of objects), what kind of data structure should i use for holding objects of job opportunities? – Ali Eghbali Oct 03 '16 at 08:17
  • I'm not sure of what you want, with Luis M Villa and Agile answers you should succeed to build the data structure you need – Michel Amorosa Oct 03 '16 at 08:41