GuaGua0308 GuaGua0308 - 1 month ago 7
reST (reStructuredText) Question

JSON Format from List

I have a RESTFul service that returns a list in JSON format. The JSON Format looks like this:

{
"cantidad": 241,
"reply": "6F5HH",
"location": "15 Azua - IP",
"str": "36%",
"date_Sent": "7/14/2017 9:58:31 AM",
"cant": 14,
"vehiculo": "",
"lc": "356612023737986"
},
{
"cantidad": 241,
"reply": "6F5HH",
"location": "15 Azua - IP",
"str": "36%",
"date_Sent": "7/20/2017 11:46:10 AM",
"cant": 1,
"vehiculo": "",
"lc": "356612023737986"
},
{
"cantidad": 241,
"reply": "5UG71",
"location": "AES NO REGISTRADO",
"str": "36%",
"date_Sent": "8/1/2017 1:35:29 PM",
"cant": 16,
"vehiculo": "",
"lc": "357042063498597"
},
{
"cantidad": 241,
"reply": "9LKYH",
"location": "AES NO REGISTRADO",
"str": "52%",
"date_Sent": "8/1/2017 1:38:09 PM",
"cant": 13,
"vehiculo": "",
"lc": "357042063498597"
},
{
"cantidad": 241,
"reply": "A3MA1",
"location": "AES NO REGISTRADO",
"str": "32%",
"date_Sent": "8/1/2017 1:38:09 PM",
"cant": 22,
"vehiculo": "",
"lc": "357042063498597"
}


As you can see, this was a 5 items List. What I need to do is generate the JSON without separating each item with a comma. So the output have to be something like this:

{
"cantidad": 241,
"reply": "6F5HH",
"location": "15 Azua - IP",
"str": "36%",
"date_Sent": "7/14/2017 9:58:31 AM",
"cant": 14,
"vehiculo": "",
"lc": "356612023737986"
}
{
"cantidad": 241,
"reply": "6F5HH",
"location": "15 Azua - IP",
"str": "36%",
"date_Sent": "7/20/2017 11:46:10 AM",
"cant": 1,
"vehiculo": "",
"lc": "356612023737986"
}
{
"cantidad": 241,
"reply": "5UG71",
"location": "AES NO REGISTRADO",
"str": "36%",
"date_Sent": "8/1/2017 1:35:29 PM",
"cant": 16,
"vehiculo": "",
"lc": "357042063498597"
}
{
"cantidad": 241,
"reply": "9LKYH",
"location": "AES NO REGISTRADO",
"str": "52%",
"date_Sent": "8/1/2017 1:38:09 PM",
"cant": 13,
"vehiculo": "",
"lc": "357042063498597"
}
{
"cantidad": 241,
"reply": "A3MA1",
"location": "AES NO REGISTRADO",
"str": "32%",
"date_Sent": "8/1/2017 1:38:09 PM",
"cant": 22,
"vehiculo": "",
"lc": "357042063498597"
}


This is my Register Method of a .NET WebApi App:

public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);

config.Formatters.Remove(config.Formatters.XmlFormatter); //DEVOLVER SOLO JSON
config.Formatters.JsonFormatter.SerializerSettings.Formatting = Formatting.Indented;
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}

Answer Source

I'm not sure why do you need to break your JSON, but still. I would use this serializer (you need reference to System.Web.Extensions):

using System.Web.Script.Serialization;
...
string jsonStr = new JavaScriptSerializer().Serialize(obj);
string notJsonStr = jsonStr.Replace("},{", "}{");
return notJsonStr;

Do not forget to config your expected content type as "text\plain" instead of "application\json", cause this result string won't be a JSON any more.