【问题标题】:How to pass variable in JSON object [duplicate]如何在 JSON 对象中传递变量 [重复]
【发布时间】:2015-04-05 15:07:09
【问题描述】:

我正在使用 C# 发送 JSON 帖子。如果我直接在请求中对值进行硬编码,一切正常。但我想以变量的形式发送它,但失败了。我尝试了不同的方法,但找不到任何解决方案。 我试图从 ID 字段中的 172024 的 'num' 变量中获取值,但在响应中我得到的是字符串,而不是值。

这是我的代码

static void Main(string[] args)
{
    //Make a Json request

    var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://IPaddress/apibxe_json.php");

    httpWebRequest.ContentType = "application/json";
    httpWebRequest.Method = "POST";

    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
    {
        string num;
        num = Convert.ToString("172024");
        Console.WriteLine(num);

        string json = "[ { \"connection\" : { \"PS\": \"99778\", \"pr\" : \"******\" }},  {\"execute\" : { \"name\" : \"NewAPI\", \"params\" : { \"Action\" : \"NEW\", \"ID\":  \"$num\" ,   \"Dlr\" : \"&&&&&\"}}}]";

        streamWriter.Write(json);
    }

    //Get the response
    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        var responseText = streamReader.ReadToEnd();

        JArray jresponse = JArray.Parse(responseText);

        Console.WriteLine(jresponse);  
    }
}

【问题讨论】:

  • 什么。在地球上。连接字符串就像第一天的东西。我强烈建议您打开一本书,例如 CLR Via C#。 \"PS\": \"" + num + "\", \"pr\" 完成。
  • 有趣的是,推荐将“CLR via C#”作为介绍性文本。 :-)
  • 对 C# 使用 JSON 库 - 有几个示例。

标签: c# json


【解决方案1】:

代码片段可能对你有帮助(在 MVC 上)....

 public JsonResult LoadName(string temp)
 {
      var fromBd=temp+" Bangladesh";
      return Json(fromBd,JsonRequestBehavior.AllowGet);
 }

jquery 函数是 ....

function(){
var temp='From';
$.get("/BasicSettings/Ajax/LoadName", { temp: temp}, function (data) {
            $('span').html(data.fromBd); 
        });
}

【讨论】:

  • 我更改了代码并且正在工作。感谢您的帮助,非常感谢。我是 C# 新手,想一步一步地学习更多内容,如果有的话,请推荐一些初中级书籍。
【解决方案2】:

作为字符串连接的替代方法,您可能需要考虑创建一个类来表示您正在写入请求正文的 JSON 并将其序列化,以便更容易使用。

我注意到您已经在使用 JSON.NET-- 下面是使用该库的方法(使用 json2csharp 生成的类。他们可以进行一些清理,但这只是一个示例):

public class Connection
{
    public string PS { get; set; }
    public string pr { get; set; }
}

public class Params
{
    public string Action { get; set; }
    public int ID { get; set; }
    public string Dlr { get; set; }
}

public class Execute
{
    public Execute()
    {
        this.Parameters = new Params();
    }

    public string name { get; set; }

    [JsonProperty("params")]
    public Params Parameters { get; set; }
}

public class Request
{
    public Request()
    {
        this.connection = new Connection();
        this.execute = new Execute();
    }

    public Connection connection { get; set; }
    public Execute execute { get; set; }
}

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    var request = new Request();

    /* Set other properties as well */
    request.execute.Parameters.ID = 172024;

    string json = JsonConvert.SerializeObject(request);

    streamWriter.Write(json);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 2021-11-26
    • 1970-01-01
    相关资源
    最近更新 更多