go for this:
install Json.Net (mvc uses it anyways but update it just in case), go here:
http://json.codeplex.com/
just open the package manager console and type 'Install-Package Newtonsoft.Json'
then create your object like so:
[DataContract]
[JsonObject]
public class ProjectJson
{
[DataMember(Name = "name")]
[JsonProperty("name")]
public string Name { get; set; }
[DataMember(Name = "id")]
[JsonProperty("id")]
public int Id { get; set; }
[DataMember(Name = "date")]
[JsonProperty("date")]
public string Date { get; set; }
[DataMember(Name = "imgageSrc")]
[JsonProperty("imgageSrc")]
public string ImgageSrc { get; set; }
[DataMember(Name = "graphImageSrc")]
[JsonProperty("graphImageSrc")]
public string GraphImageSrc { get; set; }
[DataMember(Name = "data")]
[JsonProperty("data")]
public ProjectJsonData Data { get; set; }
}
[DataContract(Name = "data")]
[JsonObject("data")]
public class ProjectJsonData
{
[DataMember(Name = "wind")]
[JsonProperty("wind")]
public int Wind { get; set; }
[DataMember(Name = "rain")]
[JsonProperty("rain")]
public int Rain { get; set; }
[DataMember(Name = "temp")]
[JsonProperty("temp")]
public int Temp { get; set; }
[DataMember(Name = "humidity")]
[JsonProperty("humidity")]
public int Humidity { get; set; }
[DataMember(Name = "sun")]
[JsonProperty("sun")]
public int Sun { get; set; }
[DataMember(Name = "alerts")]
[JsonProperty("alerts")]
public int Alerts { get; set; }
}
now use the controller code:
[Authorize]
public ActionResult GetJsonProjects(string id)
{
// id - user id
var projects = new List<ProjectJson>
{
new ProjectJson{
Name = "Avocado Site 4",
Id = 48,
Date = "12/10/2012",
ImgageSrc = "images/image.jpg",
GraphImageSrc = "images/graph-02.png",
Data = new ProjectJsonData{
Wind = 20,
Rain = 45,
Temp = 24,
Humidity = 45,
Sun = 1335,
Alerts = 3
}
}
};
return Content(JsonConvert.SerializeObject(project));
}
now if its a big json (maxJsonLength property issue) i recommend this article:
http://brianreiter.org/2011/01/03/custom-jsonresult-class-for-asp-net-mvc-to-avoid-maxjsonlength-exceeded-exception/
worked like a charm for me!
use it like so:
[Authorize]
public ActionResult GetJsonSensors(int id)
{
//some json generation code
List<string> data = new List<string>()
return new LargeJsonResult() { Data = data , MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
in addition if you need some jsonp stuff for development (hack to allow web apps to retrieve data across domains) i recommend you use:
https://gist.github.com/nikmd23/1944346
simple and easy solution , i modified it so:
added this function:
public string ExecuteResult(object data)
{
var serializer = new DataContractJsonSerializer(this.Data.GetType());
String output = String.Empty;
using (var ms = new MemoryStream())
{
serializer.WriteObject(ms, this.Data);
output = Encoding.UTF8.GetString(ms.ToArray());
}
return output;
}
and changed the response.write like so:
response.Write(string.Format("{0}({1});", this.JsonCallback, ExecuteResult(Data)));
it doesn't get easier!
if you need the jsonp to support big json dont forget to modify the web.config of the project like so:
<configuration>
.
.
.
.
</configSections>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483646" />
</webServices>
</scripting>
</system.web.extensions>
<connectionStrings>
.
.
.
.
.
</configuration>
hope iv helped!