Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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请求的一部分_C#_Json - Fatal编程技术网

C# 使用可变内容更改JSON请求的一部分

C# 使用可变内容更改JSON请求的一部分,c#,json,C#,Json,我有一个JSON代码,它向API请求一些信息,请参见以下代码 string json = "{ \"amount\" : \"0.5\", \"withdrawal\" : \"...\", \"pair\" : \"eth_xmr\" }"; 实际上,我想将“0.5”信息替换为从数学计算课上获得的可变内容 我尝试了一些东西,但我不能让它工作(每次红线) 如何将“0.5”部分替换为字符串变量 编辑: 以下是完整的呼叫代码: var httpWebRequest = (HttpWebReques

我有一个JSON代码,它向API请求一些信息,请参见以下代码

string json = "{ \"amount\" : \"0.5\", \"withdrawal\" : \"...\", \"pair\" : \"eth_xmr\" }";
实际上,我想将“0.5”信息替换为从数学计算课上获得的可变内容

我尝试了一些东西,但我不能让它工作(每次红线)

如何将“0.5”部分替换为字符串变量

编辑:

以下是完整的呼叫代码:

var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
            httpWebRequest.ContentType = "application/json; charset=utf-8";
            httpWebRequest.Method = "POST";

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = "{ \"amount\" : \"0.5\", \"withdrawal\" : \"...\", \"pair\" : \"eth_xmr\" }";

                streamWriter.Write(json);
                streamWriter.Flush();
            }
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var responseText = streamReader.ReadToEnd();
                var apiResponse = JsonConvert.DeserializeObject<ApiResponse>(responseText);
var httpWebRequest=(httpWebRequest)WebRequest.Create(webAddr);
httpWebRequest.ContentType=“application/json;charset=utf-8”;
httpWebRequest.Method=“POST”;
使用(var streamWriter=newstreamwriter(httpWebRequest.GetRequestStream()))
{
字符串json=“{\'金额\':\'0.5\”、\'提款\':\“…\”、\'对\':\“eth\U xmr\”;
streamWriter.Write(json);
streamWriter.Flush();
}
var httpResponse=(HttpWebResponse)httpWebRequest.GetResponse();
使用(var streamReader=newstreamreader(httpResponse.GetResponseStream())
{
var responseText=streamReader.ReadToEnd();
var apiResponse=JsonConvert.DeserializeObject(responseText);
这应该可以:

double amount = 0.75;
string json = $"{{ \"amount\" : \"{amount}\", \"withdrawal\" : \"...\", \"pair\" : \"eth_xmr\" }}";
不要忘记字符串前面的
$
。然后您可以访问大括号
{}

编辑:您必须在开始时使用
{{
,在结束时使用
}
来转义括号

这应该可以:

double amount = 0.75;
string json = $"{{ \"amount\" : \"{amount}\", \"withdrawal\" : \"...\", \"pair\" : \"eth_xmr\" }}";
不要忘记字符串前面的
$
。然后您可以访问大括号
{}


编辑:您必须在开始时使用
{{
,在结束时使用
}
来转义括号

,因为您已经在使用JSON.NET,您不应该手动创建请求。而应该像这样组合JSON对象:

JObject json = new JObject();
json.Add("amount", YourCalculation());
// add your other properties the same way ...
public class MyJson
{
    [JsonProperty("pair")]
    public string Pair { get; set; }

    [JsonProperty("amount")]
    public string Amount { get; set; }

    [JsonProperty("withdrawal")]
    public string Withdrawal { get; set; }
}

如果您确实需要将此对象作为
字符串
,您可以在最后对其调用
ToString

由于您已经在使用JSON.NET,您不应该手动创建请求。而应该像这样编写JSON对象:

JObject json = new JObject();
json.Add("amount", YourCalculation());
// add your other properties the same way ...
public class MyJson
{
    [JsonProperty("pair")]
    public string Pair { get; set; }

    [JsonProperty("amount")]
    public string Amount { get; set; }

    [JsonProperty("withdrawal")]
    public string Withdrawal { get; set; }
}

如果您确实需要将此对象作为
字符串
,则可以在最后对其调用
ToString

虽然您当然可以操纵字符串来创建json,但最好的方法是使用模型(类)使用所有这些字段,将值设置为模型,然后在调用api之前将其序列化为json。

虽然您当然可以操作字符串来创建json,但最好的方法是使用模型(类)使用所有这些字段,将值设置为模型,然后在调用api之前将其序列化为json。

使用json.NET并转换json字符串并处理金额:

string json = "{ \"amount\" : \"0.5\", \"withdrawal\" : \"...\", \"pair\" : \"eth_xmr\" }";

dynamic lsDynObject = JsonConvert.DeserializeObject(json);
lsDynObject.amount = 605.123;
string lsOut = JsonConvert.SerializeObject(lsDynObject);

Console.WriteLine(lsOut);
安装时使用:

安装软件包Newtonsoft.Json

但是你必须考虑到数量是一个数字。 如果继续使用金额作为字符串,则必须设置:

lsDynObject.amount = "605.123";
更新:

使用这样的类:

JObject json = new JObject();
json.Add("amount", YourCalculation());
// add your other properties the same way ...
public class MyJson
{
    [JsonProperty("pair")]
    public string Pair { get; set; }

    [JsonProperty("amount")]
    public string Amount { get; set; }

    [JsonProperty("withdrawal")]
    public string Withdrawal { get; set; }
}
并用以下代码编写json:

streamWriter.Write(JsonConvert.SerializeObject(new MyJson()
{
    Pair = "eth_xmr",
    Withdrawal = "...",
    Amount = "34.676"
}));

使用Json.NET并转换Json字符串,并操纵金额:

string json = "{ \"amount\" : \"0.5\", \"withdrawal\" : \"...\", \"pair\" : \"eth_xmr\" }";

dynamic lsDynObject = JsonConvert.DeserializeObject(json);
lsDynObject.amount = 605.123;
string lsOut = JsonConvert.SerializeObject(lsDynObject);

Console.WriteLine(lsOut);
安装时使用:

安装软件包Newtonsoft.Json

但是你必须考虑到数量是一个数字。 如果继续使用金额作为字符串,则必须设置:

lsDynObject.amount = "605.123";
更新:

使用这样的类:

JObject json = new JObject();
json.Add("amount", YourCalculation());
// add your other properties the same way ...
public class MyJson
{
    [JsonProperty("pair")]
    public string Pair { get; set; }

    [JsonProperty("amount")]
    public string Amount { get; set; }

    [JsonProperty("withdrawal")]
    public string Withdrawal { get; set; }
}
并用以下代码编写json:

streamWriter.Write(JsonConvert.SerializeObject(new MyJson()
{
    Pair = "eth_xmr",
    Withdrawal = "...",
    Amount = "34.676"
}));


@TavishAggarwal有效。你有一个语法错误。你真的因为这个而否决了它吗?@TavishAggarwal很公平。对不起,你必须在开头和结尾用大括号来准确地转义它们!你不应该用它来创建jsonhand@TavishAggarwal有效。你有语法错误。你真的因为这个而否决了它吗?@TavishAggarwal很公平。对不起,你必须在开头和结尾都用大括号才能准确地避开它们!你不应该手工创建json为什么你要手工创建json?我建议使用众多json库中的一个。默认值NET的一个是JSON.NET。我使用JSON.NET将答案放入某个字符串变量中。我只是用它来调用APIC。为什么要手动创建JSON?我建议使用众多JSON库中的一个。默认.NET的一个变量是JSON.NET。我使用JSON.NET将答案放入某个字符串变量。我只是用它来调用API。您的代码正在运行,但我无法使用我的变量的数量。知道为什么吗?您的
amout
变量可能是一个数字。将其转换为字符串或将
MyJson.amout
更改为您的数字类型。但是如果您ange to a number type引号在您的json字符串中丢失。它实际上是一个字符串,但我无法使其在金额行中工作,我不知道为什么。它在文本框中工作,但在json请求中不工作。您能在您的问题中添加一个示例吗?我实际上使用的是从API获取价格。此信息以浮点形式保存变量“USD”。我创建了一个计算所需价格的方法(一些数学)在同一个方法中,它将浮点值转换为返回的字符串。我从该方法中得到一个字符串值下的新结果,我想在JSON请求中使用这个值。它实际上起作用了,因为我可以在文本框中使用变量看到它,但当我将它与代码“Amount=variable”一起使用时它不起作用,API发送了一个错误。那么我如何才能使它起作用呢?您的代码起作用了,但我不能在数量上使用我的变量。知道为什么吗?您的
amout
变量可能是一个数字。将其转换为字符串或将
MyJson.amout
更改为您的数字类型。但是,如果您更改为数字类型,则引号是lost在json字符串中。它实际上是一个字符串,但我无法使它在金额行中工作,我不知道为什么。它在文本框中工作,但在json请求中不工作。你能在你的问题中添加一个示例吗?我实际上正在使用GE