相关疑难解决方法(0)

将JSON字符串反序列化为c#对象

我的应用程序是在用C#编码的Asp.Net MVC3中.这就是我的要求.我想要一个以下格式的对象.当我反序列化Json字符串时,应该实现这个对象.

var obj1 = new { arg1=1,arg2=2 };
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

使用以下代码后:

string str = "{\"Arg1\":\"Arg1Value\",\"Arg2\":\"Arg2Value\"}";
JavaScriptSerializer serializer1 = new JavaScriptSerializer();
object obje = serializer1.Deserialize<object>(str);
Run Code Online (Sandbox Code Playgroud)

我得到的对象即obje不作为obj1

在此输入图像描述

在这个示例中,我的JSON字符串是静态的,但实际上JSON字符串将是动态生成的运行时,因此我无法一直获得Arg1和Arg2.

c# json c#-4.0 asp.net-mvc-3

21
推荐指数
3
解决办法
9万
查看次数

如何构建要发送到AJAX WebService的JSON对象?

在尝试使用javascript手动格式化我的JSON数据并且失败后,我意识到可能有更好的方法.以下是C#中Web服务方法和相关类的代码:

[WebMethod]
public Response ValidateAddress(Request request)
{
    return new test_AddressValidation().GenerateResponse(
        test_AddressValidation.ResponseType.Ambiguous);
}

...

public class Request
{
    public Address Address;
}

public class Address
{
    public string Address1;
    public string Address2;
    public string City;
    public string State;
    public string Zip;
    public AddressClassification AddressClassification;
}

public class AddressClassification
{
    public int Code;
    public string Description;
}
Run Code Online (Sandbox Code Playgroud)

Web服务使用SOAP/XML很有效,但我似乎无法使用javascript和jQuery获得有效的响应,因为我从服务器返回的消息与我的手工编码的JSON有问题.

我不能使用jQuery getJSON函数,因为请求需要HTTP POST,所以我使用的是低级ajax函数:

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "http://bmccorm-xp/HBUpsAddressValidation/AddressValidation.asmx/ValidateAddress",
    data: "{\"Address\":{\"Address1\":\"123 Main Street\",\"Address2\":null,\"City\":\"New York\",\"State\":\"NY\",\"Zip\":\"10000\",\"AddressClassification\":null}}",
    dataType: "json",
    success: function(response){
        alert(response); …
Run Code Online (Sandbox Code Playgroud)

javascript c# jquery json web-services

15
推荐指数
2
解决办法
3万
查看次数

asp.net jquery ajax json:交换数据的简单例子

(在两个回复帖子的帮助下解决问题 - 见下文)

我将非常感谢帮助获取在浏览器(使用JavaScript/JQuery)和ASP.NET(使用Visual Studio 2010)之间交换数据JSON数据的简单示例.

当我单击按钮时,执行以下操作:

 <script type="text/javascript">
    bClick = function () {
        var myData = { "par": "smile" };
        alert("hi "+myData.par);
        $.ajax({
            url: "ericHandler.ashx",
            data: myData,
            dataType: 'json',
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            success: function (data) { alert("DIDit = " + data.eric); },
            error: function (data, status, jqXHR) { alert("FAILED:" + status); }
        });
    }
</script>
Run Code Online (Sandbox Code Playgroud)

在Visual Studio中,我有以下与ashx文件关联的代码.当我运行它并单击按钮时,一切都按预期工作,除了我没有看到myData传递给C#代码 - 我在调试器中查看context.Request.QueryString并显示"{}".

我见过使用的例子

string stringParam = (string)Request.Form("stringParam");
Run Code Online (Sandbox Code Playgroud)

但似乎没有定义Visual Studio"请求".我想做的就是看到数据双向移动,而我似乎只有一半.任何帮助,将不胜感激.

--C#代码 -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace …
Run Code Online (Sandbox Code Playgroud)

asp.net ajax jquery json

4
推荐指数
2
解决办法
3万
查看次数

支持JSONP的ASP.NET通用HTTP处理程序(.ashx)

有人可以显示返回JSON并支持跨域调用的HTTP处理程序的示例.我正在使用jQuery的getJSON()将请求发送到我的Web服务器上的.ashx文件.

我明白我需要添加?callback =?在getJSON()url中我的url,但我不确定在我的ashx文件中需要在服务器上做什么?

asp.net ajax json jsonp

3
推荐指数
1
解决办法
5727
查看次数

在asp.net中使用javascript传递变量值调用c#后面的函数

这是我的场景:我有一个可以合并tiff文件的asp网站.所以,要做到这一点,我需要使用ac#函数,并在javascript事件后调用它.c#是这样的:

public void mergeImages(string initialUrl, string lastImageUrl)
        {....}
Run Code Online (Sandbox Code Playgroud)

我创建了两个这样的隐藏字段:

 <input type="hidden" id="hi1" value="D:\\ProvaUpload\\1.tif" />
Run Code Online (Sandbox Code Playgroud)

获取值传递给函数,因为我不知道我可以通过哪种方式将js变量传递给它.我用这种方式调用函数:

'<%mergeImages(par1,par2); %>';
Run Code Online (Sandbox Code Playgroud)

我可以通过哪种方式将变量值传递给函数?

javascript c# asp.net

1
推荐指数
1
解决办法
6937
查看次数

标签 统计

json ×4

asp.net ×3

c# ×3

ajax ×2

javascript ×2

jquery ×2

asp.net-mvc-3 ×1

c#-4.0 ×1

jsonp ×1

web-services ×1