Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C将JSON代码导入Javascript文件#_Javascript_C#_Asp.net_Json - Fatal编程技术网

使用C将JSON代码导入Javascript文件#

使用C将JSON代码导入Javascript文件#,javascript,c#,asp.net,json,Javascript,C#,Asp.net,Json,我用NuGet数据包NewtonsoftJsonConvert生成了一些JSON代码。但我现在想要的是将代码导入到javascript文件中,这样我就可以创建一个google地图 我的Json代码如下所示: [ { "title": 'Alibaug', "lat": '18.641400', "lng": '72.872200', "description": 'Alibaug is a coastal town and

我用NuGet数据包Newtonsoft
JsonConvert
生成了一些JSON代码。但我现在想要的是将代码导入到javascript文件中,这样我就可以创建一个google地图

我的Json代码如下所示:

[
    {
        "title": 'Alibaug',
        "lat": '18.641400',
        "lng": '72.872200',
        "description": 'Alibaug is a coastal town and a municipal council in Raigad District in the Konkan region of Maharashtra, India.'
    },
    {
        "title": 'Mumbai',
        "lat": '18.964700',
        "lng": '72.825800',
        "description": 'Mumbai formerly Bombay, is the capital city of the Indian state of Maharashtra.'
    },
    {
        "title": 'Pune',
        "lat": '18.523600',
        "lng": '73.847800',
        "description": 'Pune is the seventh largest metropolis in India, the second largest in the state of Maharashtra after Mumbai.'
    }
];
var myJsonVariable = $.parseJSON('<%=MyApplication.MyWebPage.getMapJson()%>');
我在asp.net表单项目中使用的语言是C

请看一下我前面的问题:

有什么办法解决我的问题吗

感谢您向asp页面发出ajax请求,呈现JSON


向asp页面发出ajax请求,呈现JSON


向asp页面发出ajax请求,呈现JSON


向asp页面发出ajax请求,呈现JSON


如果您使用的是MVC,您可以执行以下操作:

public Coordinates
{
    public string Title { get; set; }
    public string Lat { get; set; }
    public string Lng { get; set; }
    public string Description { get; set; }
}

public ActionResult GetCoordinates(){

    // get your coordinates
    List<Coordinates> coordinates = GetCoordinatesFromDatabase();

    var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    var objectAsJsonString = serializer.Serialize(coordinates );

    return Json(objectAsJsonString);
}

我希望这对您有所帮助。

如果您使用的是MVC,您可以这样做:

public Coordinates
{
    public string Title { get; set; }
    public string Lat { get; set; }
    public string Lng { get; set; }
    public string Description { get; set; }
}

public ActionResult GetCoordinates(){

    // get your coordinates
    List<Coordinates> coordinates = GetCoordinatesFromDatabase();

    var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    var objectAsJsonString = serializer.Serialize(coordinates );

    return Json(objectAsJsonString);
}

我希望这对您有所帮助。

如果您使用的是MVC,您可以这样做:

public Coordinates
{
    public string Title { get; set; }
    public string Lat { get; set; }
    public string Lng { get; set; }
    public string Description { get; set; }
}

public ActionResult GetCoordinates(){

    // get your coordinates
    List<Coordinates> coordinates = GetCoordinatesFromDatabase();

    var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    var objectAsJsonString = serializer.Serialize(coordinates );

    return Json(objectAsJsonString);
}

我希望这对您有所帮助。

如果您使用的是MVC,您可以这样做:

public Coordinates
{
    public string Title { get; set; }
    public string Lat { get; set; }
    public string Lng { get; set; }
    public string Description { get; set; }
}

public ActionResult GetCoordinates(){

    // get your coordinates
    List<Coordinates> coordinates = GetCoordinatesFromDatabase();

    var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    var objectAsJsonString = serializer.Serialize(coordinates );

    return Json(objectAsJsonString);
}

我希望这对您有所帮助。

要做到这一点,请发出ajax请求或使用.aspx页面上的符号。第二个更容易:)

例如:

我有一个getMapJson()方法,它在MyWebPage.aspx页面的代码隐藏中返回我的Json。重要的是要使它保持静态,否则您将无法从MyWebPage.aspx调用它

namespace MyApplication
{
    public partial class MyWebPage : System.Web.UI.Page
    {
        public static string getMapJson()
        {
             //your code
             return json;
        }
    }
}
在MyWebPage.aspx页面的脚本中,执行以下操作:

[
    {
        "title": 'Alibaug',
        "lat": '18.641400',
        "lng": '72.872200',
        "description": 'Alibaug is a coastal town and a municipal council in Raigad District in the Konkan region of Maharashtra, India.'
    },
    {
        "title": 'Mumbai',
        "lat": '18.964700',
        "lng": '72.825800',
        "description": 'Mumbai formerly Bombay, is the capital city of the Indian state of Maharashtra.'
    },
    {
        "title": 'Pune',
        "lat": '18.523600',
        "lng": '73.847800',
        "description": 'Pune is the seventh largest metropolis in India, the second largest in the state of Maharashtra after Mumbai.'
    }
];
var myJsonVariable = $.parseJSON('<%=MyApplication.MyWebPage.getMapJson()%>');
var myJsonVariable=$.parseJSON(“”);
现在,在myJsonVariable中有了已经反序列化的Json:)


您也可以导入静态属性而不是方法,但您必须在页面加载之前设置其值,因为每个页面加载只计算一次内部内容,因此除非重新加载,否则它不会更改。

为此,请发出ajax请求或在.aspx页面上使用符号。第二个更容易:)

例如:

我有一个getMapJson()方法,它在MyWebPage.aspx页面的代码隐藏中返回我的Json。重要的是要使它保持静态,否则您将无法从MyWebPage.aspx调用它

namespace MyApplication
{
    public partial class MyWebPage : System.Web.UI.Page
    {
        public static string getMapJson()
        {
             //your code
             return json;
        }
    }
}
在MyWebPage.aspx页面的脚本中,执行以下操作:

[
    {
        "title": 'Alibaug',
        "lat": '18.641400',
        "lng": '72.872200',
        "description": 'Alibaug is a coastal town and a municipal council in Raigad District in the Konkan region of Maharashtra, India.'
    },
    {
        "title": 'Mumbai',
        "lat": '18.964700',
        "lng": '72.825800',
        "description": 'Mumbai formerly Bombay, is the capital city of the Indian state of Maharashtra.'
    },
    {
        "title": 'Pune',
        "lat": '18.523600',
        "lng": '73.847800',
        "description": 'Pune is the seventh largest metropolis in India, the second largest in the state of Maharashtra after Mumbai.'
    }
];
var myJsonVariable = $.parseJSON('<%=MyApplication.MyWebPage.getMapJson()%>');
var myJsonVariable=$.parseJSON(“”);
现在,在myJsonVariable中有了已经反序列化的Json:)


您也可以导入静态属性而不是方法,但您必须在页面加载之前设置其值,因为每个页面加载只计算一次内部内容,因此除非重新加载,否则它不会更改。

为此,请发出ajax请求或在.aspx页面上使用符号。第二个更容易:)

例如:

我有一个getMapJson()方法,它在MyWebPage.aspx页面的代码隐藏中返回我的Json。重要的是要使它保持静态,否则您将无法从MyWebPage.aspx调用它

namespace MyApplication
{
    public partial class MyWebPage : System.Web.UI.Page
    {
        public static string getMapJson()
        {
             //your code
             return json;
        }
    }
}
在MyWebPage.aspx页面的脚本中,执行以下操作:

[
    {
        "title": 'Alibaug',
        "lat": '18.641400',
        "lng": '72.872200',
        "description": 'Alibaug is a coastal town and a municipal council in Raigad District in the Konkan region of Maharashtra, India.'
    },
    {
        "title": 'Mumbai',
        "lat": '18.964700',
        "lng": '72.825800',
        "description": 'Mumbai formerly Bombay, is the capital city of the Indian state of Maharashtra.'
    },
    {
        "title": 'Pune',
        "lat": '18.523600',
        "lng": '73.847800',
        "description": 'Pune is the seventh largest metropolis in India, the second largest in the state of Maharashtra after Mumbai.'
    }
];
var myJsonVariable = $.parseJSON('<%=MyApplication.MyWebPage.getMapJson()%>');
var myJsonVariable=$.parseJSON(“”);
现在,在myJsonVariable中有了已经反序列化的Json:)


您也可以导入静态属性而不是方法,但您必须在页面加载之前设置其值,因为每个页面加载只计算一次内部内容,因此除非重新加载,否则它不会更改。

为此,请发出ajax请求或在.aspx页面上使用符号。第二个更容易:)

例如:

我有一个getMapJson()方法,它在MyWebPage.aspx页面的代码隐藏中返回我的Json。重要的是要使它保持静态,否则您将无法从MyWebPage.aspx调用它

namespace MyApplication
{
    public partial class MyWebPage : System.Web.UI.Page
    {
        public static string getMapJson()
        {
             //your code
             return json;
        }
    }
}
在MyWebPage.aspx页面的脚本中,执行以下操作:

[
    {
        "title": 'Alibaug',
        "lat": '18.641400',
        "lng": '72.872200',
        "description": 'Alibaug is a coastal town and a municipal council in Raigad District in the Konkan region of Maharashtra, India.'
    },
    {
        "title": 'Mumbai',
        "lat": '18.964700',
        "lng": '72.825800',
        "description": 'Mumbai formerly Bombay, is the capital city of the Indian state of Maharashtra.'
    },
    {
        "title": 'Pune',
        "lat": '18.523600',
        "lng": '73.847800',
        "description": 'Pune is the seventh largest metropolis in India, the second largest in the state of Maharashtra after Mumbai.'
    }
];
var myJsonVariable = $.parseJSON('<%=MyApplication.MyWebPage.getMapJson()%>');
var myJsonVariable=$.parseJSON(“”);
现在,在myJsonVariable中有了已经反序列化的Json:)


您也可以导入静态属性而不是方法,但您必须在页面加载之前设置其值,因为每个页面加载只计算一次内部内容,因此除非重新加载,否则不会更改。

您的json是有效的javascript对象<代码>var arr=[…];警报(arr[0]。标题)您的json是有效的javascript对象<代码>var arr=[…];警报(arr[0]。标题)您的json是有效的javascript对象<代码>var arr=[…];警报(arr[0]。标题)您的json是有效的javascript对象<代码>var arr=[…];警报(arr[0]。标题)不,我不使用MVC,它是asp表单项目不,我不使用MVC,它是asp表单项目不,我不使用MVC,它是asp表单项目不,我不使用MVC,它是asp表单项目