序列化功能使用C#在JSON参数 [英] Serializing a function as a parameter in json using C#

查看:154
本文介绍了序列化功能使用C#在JSON参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建创建一个使用C#jQuery中的对象所需的JSON。所需的JSON是



  {
标题:标题文本,
upperVal:40,
lowerVal:5,
鼠标悬停:功能(){回报差异+(upperVal - lowerVal); }
}



最初的几个因素是非常简单。我创建了一个类来表示对象,JSObj,然后通过JavascriptSerializer.Serialize运行这个()

 公共类JSObj {
酒店的公共字符串的标题{搞定;组; }
公众诠释upperVal {搞定;组; }
公众诠释lowerVal {搞定;组; }
}

这正常工作的前几个属性,但我没有。线索如何返回正确的鼠标悬停功能



编辑:提供的代码只是示例代码,因为我实际使用JSON的结构更复杂一点。我使用HighCharts,和我真的需要使用配置选项之一,需要一个功能,即使他们是不是真的有效的JSON(的 http://www.highcharts.com/ref/#tooltip--formatter ),所以不幸的是我无法回避的问题。


< DIV CLASS =h2_lin>解决方案

我试图完成类似的东西。
在我来说,我使用MVC剃刀语法试图生成与使用@<传递函数的JSON对象;文本>句法。



我是能够得到使用Json.net库(使用JsonConvert和JRaw)所需的输出。
http://james.newtonking.com/projects/json/help/html/ SerializeRawJson.htm



例如:

 公共类JSObj 
{
公共字符串名称{搞定;组; }
公众诠释UpperVal {搞定;组; }
公众诠释LowerVal {搞定;组; }
公共对象鼠标悬停
{
得到
{
//使用JRaw设置匿名函数
的值返回新JRaw(的String.format (@功能(){{返回{0};}},UpperVal - LowerVal));
}
}
}

//然后序列化使用JsonConvert类
VAR OBJ =新JSObj {标题=测试,LowerVal = 4,UpperVal = 10};
VAR jsonObj = JsonConvert.SerializeObject(OBJ);

这应该让你的功能(而不是在一个字符串函数)JSON对象。

  {标题:测试,UpperVal:10,LowerVal:4,鼠标悬停:功能( ){返回6; }} 



帖子:
如何序列化到JSON的函数(使用剃刀@<文本>)


I am trying to create the json needed to create an object in jQuery using C#. The json needed is

{
    title: 'title text',
    upperVal: 40,
    lowerVal: 5,
    mouseover: function() { return 'difference ' + (upperVal - lowerVal); }
}

The first few elements were simple enough. I created a class to represent the object, JSObj, and then run this through JavascriptSerializer.Serialize()

public class JSObj {
    public string title { get; set; }
    public int upperVal { get; set; }
    public int lowerVal { get; set; }
}

This works fine for the first few attributes, but I don't have a clue how to return the correct mouseover function.

EDIT: The code provided is just sample code because the structure of the json I'm actually using is a bit more complicated. I'm using HighCharts, and one of the config options that I really need to use requires a function, even though they are not really valid json ( http://www.highcharts.com/ref/#tooltip--formatter ) so unfortunately I can't avoid the problem

解决方案

I was trying to accomplish something similar. In my case I was using MVC Razor syntax trying to generate a json object with a function passed in using the @<text> syntax.

I was able to get the desired output using the Json.net library (using JsonConvert and JRaw). http://james.newtonking.com/projects/json/help/html/SerializeRawJson.htm

Example:

public class JSObj
{
    public string Title { get; set; }
    public int UpperVal { get; set; }
    public int LowerVal { get; set; }
    public object MouseOver
    {
        get
        {
            // use JRaw to set the value of the anonymous function
            return new JRaw(string.Format(@"function(){{ return {0}; }}", UpperVal - LowerVal));
        }
    }
}

// and then serialize using the JsonConvert class
var obj = new JSObj { Title = "Test", LowerVal = 4, UpperVal = 10 };
var jsonObj = JsonConvert.SerializeObject(obj);

That should get you the json object with the function (instead of the function in a string).

{"Title":"Test","UpperVal":10,"LowerVal":4,"MouseOver":function(){ return 6; }}

Post: How to serialize a function to json (using razor @<text>)

这篇关于序列化功能使用C#在JSON参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆