sarsnake
sarsnake

Reputation: 27733

Json web services return format

It may be an easy question but I am wondering how do I get a JSON web service to return the string that has a format like this:

var invoice = {
  invoiceItems: [
    { type: 'item', 
      part: '99Designs', description: '99 Designs Logo', 
      price: 450.00, qty: 1 },
    { type: 'service',
      service: 'Web development and testing', 
      price: 25000.00 },
    { type: 'item',
      part: 'LinodeMonthly', description: 'Monthly site hosting', 
      price: 40.00, qty: 12 }
  ]
};

What my service returns now is List and it looks like

[1,2,2,4444]
[13,444,233]
etc

Does the part prior to ':' get automatically appended or do I need to manually do it inside the web service?

Upvotes: 0

Views: 502

Answers (1)

David Tang
David Tang

Reputation: 93694

JSON is just text with a particular syntax. That means your web service needs to output text that looks exactly like:

{
  invoiceItems: [
    { type: 'item', 
      part: '99Designs', description: '99 Designs Logo', 
      price: 450.00, qty: 1 },
    { type: 'service',
      service: 'Web development and testing', 
      price: 25000.00 },
    { type: 'item',
      part: 'LinodeMonthly', description: 'Monthly site hosting', 
      price: 40.00, qty: 12 }
  ]
}

You'll most likely find helpful libraries to convert objects to JSON format (it'll work like a specialised .toString()), depending on the language used for the web service. But you'll have to transform the Lists you have right now into something more like a Map in order for the library to output the JSON automatically.

Alternatively, you can build up your own string by iterating through your Lists and simply returning that string as the output.

Upvotes: 3

Related Questions