I am currently building a web app with ember.js and webapi 3.
Currently I am facing the problem that webapi sends a JSON result (of an array of DTOs) which doesn't get understood by ember because it is missing a root element.
I didn't got far with the solutions on the web, which mostly just pointed to a template project for VS.
Has someone a simple and understandable solution?
I'm going to assume you're talking about Ember-Data.
web-api sends back json in whatever format the object is, so if you're sending back a list/array it's going to return an array of objects. If you have control of the controller then just modify what it's returning.
return new { foos = new List<string>() };
Related
i am a programming student, working with a asp.net mvc 4 web app, i am testing a simple web app that uses the http://openweathermap.org/api so when i click a link, it executes the Action Result on the home controller, the code to get the data from the api is in this action result and it fetches the api json data and stores it in a viewbag.message that is then passed back to the view. I have that much working in so far as i can get the json result and see it on the view. I am not sure how to proceed & the pages i have looked up seem to be centered around ajax jquery
My question is about the serializing/displaying the json result without adding any plugins or anything just yet, i would like to format / serialise it so it looks better maybe fit it into a table or something, but without getting into ajax & jquery just yet as i haven't learned that part yet.
I have removed the api key from the code.
public ActionResult getWeather()
{
var uri = "http://api.openweathermap.org/data/2.5/forecast?id=7778677&APPID=123456789";
WebClient client = new WebClient();
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
var resultContent = client.DownloadString(uri);
ViewBag.Message = resultContent;
return View();
}
And here is the result view:
screenshot of result view showing the json data from the api
I hope i am explaining myself correctly,
Thank You.
M.
.NET 4 has a built-in JSON serializer/deserializer, but if you can, use NuGet to get NewtonSofts JSON Framework as it is much more flexible and powerful.
With it, you could do something along the lines of this within your backing code for your view, or maybe even in your view's template with Razor:
WeatherDataModel[] myWeatherData = JsonConvert.DeserializeObject<WeatherDataModel>(ViewBag.Message);
(This assumes you have a model for the weather data that matches up with the data you get from the openweathermap api.)
You should then be able to use a loop to get the data in your WeatherDataModel array into a table.
Another option, if you are willing to take a small leap into jquery, can be found in the answer to this question:
How to load JSON data into Bootstrap table?
It makes use of a very simple snippet of javascript/jquery and bootstrap.
In your case, you would set the data to use the JSON you stored in the ViewBag.Message:
$(function () {
$('#table').bootstrapTable({
data: ViewBag.Message
});
});
If you built your app in VS 2015 and used the MVC 4 template, you may very well already have bootstrap built into your project. If not, you can easily import it with NuGet.
I haven't done much in MVC 4 for a while now, and so I will point you to this question's answer about where best to place your scripts: Proper place to load jQuery in MVC Layout view
Good luck!
I am looking for either guidance or a good example where I can map data coming from rest services to JSON "type" object which can then be used in a number of different react components.
The JSON Object will be used to map data from a few different rest services, which essentially hold very similar data which makes it better to use one object and then to bind the data to the respective React Components.
I am fairly new to React.JS and I have googled around to find a data mapper to JSON from Rest Service example.
Can anyone help?
You typically don't have to do too much, at least on the front end side. As long as the REST endpoint can return JSON responses you'll be fine. Just make sure you set the appropriate Content-Type headers in the request. Note that setting the header doesn't guarantee a JSON response, the server has to be able to send it in that format.
If you're creating the REST service yourself, you have many options. If you're using node, you can simply return a javascript object. If you're using some other language like Java, C#, etc., they come with libraries that can serialize objects into JSON for you. I use JSON.net when working with C#. In these cases, because the data will be returned as a string, you'll just need to JSON.parse() it upon receiving it and then set it to the appropriate React component's state.
Ok, this is my Nth question regarding this topic, and I'm getting really frustrated with Grails. Please have a quick look on one of my earlier questions for more details.
Among other things, my problem is that sending JSON formatted data to the controller when testing doesn't seem to work. The controller doesn't get null object, but the argument passed is practically empty--the JSON properties don't get set.
Aside from the controller code from the link above, I also tried,
def save() {
def model = new MyModel(request.JSON)
model.save()
}
but it still fails to set properties.
From my Web searches, I read that in older versions, parseRequest must be set to true in UrlMapping.groovy so that request data formatted in XML, JSON, etc. would automatically be parsed and passed as controller method argument. I'm working on Grails 2.3.9, and I'm not sure if it's still necessary to do that.
The time I thought I'd save if I use Grails on this project is being spent on looking for an answer to this seemingly simple task of testing a RESTful Web service.
No since 2.3.0 the parseRequest option doesn't do anything. The request is parsed lazily only when request.XML or request.JSON is accessed or when binding to a command object.
I have a Restlet application already working that accepts JSON and returns JSON entity as response.
I'm trying to understand how I can compress the JSON entity that is returned in the response.
I did not find any clear example on how to achieve it.
I think I have to put somewhere on the router chain the Encoder/EncoderService classes, but I really don't understand where and how to use them.
Could anybody help me?
After some testing, I got the answer.
Creating a new filter like this
Filter encoder = new Encoder(getContext(), false, true, new EncoderService(true));
inside the createInboundRoot() method of my own Application class did the trick, the client requests were already containing the gzip header needed.
I would be wanting a tool which can mock a RESTFul server and it should return the preset JSON data which I have mapped for a particular URL.
For ex: If I call http://ccccc.com/api/users , the mockup tool should return users ( which I have already preset ) in JSON format.
This might be useful when I create client side code with backbone or jquery to get back the models using an ajax call.
Any suggestions for this kind of mockup tool ?
Note: Creating a servlet which reads the incoming GET url and reads the preset JSON from a file and outputting it as JSON string is possible. But I am looking for a tool which will do this for me.
Sinon.js is what you're looking for. In particular the sinon.fakeServer API.
https://github.com/homerquan/kakuen
Mock up RESTful webservices simply by editing text files, e.g.,
GET_#book#123#authors.json ==> GET /book/123/authors
POST_#book#id=123.json ==> POST /book?id=123
For json, a schema-based mockup is supported, e.g., in sample_server/mocks/GET__#search#q=js.json
e.g.,
"#KAKUEN_ITEM(offset)": {
"#KAKUEN_TYPE": "natural",
"#KAKUEN_PARAM": {
"min": 1,
"max": 20
}
}
will be offset:12