如何将C#字符串插入JSON字符串

3

如何将字符串sUsername插入到myEmail中,将sPassword插入到myPassword中?

sUsernamesPassword来自登录表单。

我正在学习JSON,我能否创建一个JSON变量并用sUsernamesPassword的值填充它?

以下是我的代码:

public void login(string sUsername, string sPassword)
{
    var httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("http://atsiri.online/api/v1.php");
    httpWebRequest.ContentType = "application/json";
    httpWebRequest.Method = "POST";

    using (var streamWriter = new System.IO.StreamWriter(httpWebRequest.GetRequestStream()))
    {
        string json = @"
        {
            ""type"":""login"",
            ""condition"":{
                ""0"":{
                    ""key"":""email"",
                    ""value"":""myEmail""
                },
                ""1"":{
                    ""key"":""password"",
                    ""value"":""myPassword""
                }
            }
        }";

        streamWriter.Write(json);
        streamWriter.Flush();
        streamWriter.Close();
    }

    var httpResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();
    using (var streamReader = new System.IO.StreamReader(httpResponse.GetResponseStream()))
    {
        var result = streamReader.ReadToEnd();
        Console.WriteLine(result.ToString());
    }
}

你手动处理而不使用Json.NET的原因是什么?我强烈建议你使用JSON库而不是自己构建字符串。 - Jon Skeet
FYI,在.Net中有一个广泛使用的用于反/序列化JSON的库,网址为https://www.newtonsoft.com/json。 - Toby
4个回答

1
我认为你的JSON字符串无效,你应该正确格式化它(请参考此链接)。
首先制作一个原始JSON,然后使用链接来获取转义后的JSON字符串。
你可以使用以下代码追加字符串:
var json = "{   \"type\": \"login\",    \"condition\": {        \"0\": {            \"key\": \"email\",         \"value\": \"" + sUsername + "\"        },      \"1\": {            \"key\": \"password\",          \"value\": \"" + password + "\"     }   }}";

enter image description here

=======更新=====

根据Jon Skeet的建议,使用JSON库来构建请求的JSON对象。这里是一个基于你的情况的示例代码。我使用了NuGet中的Newtonsoft.Json库。

1.创建一个类:

public class LoginRequest
{
    public string Type { get; set; }
    public Dictionary<string,string> [] condition { get; set; }
}

2. 使用Newtonsoft.Json库序列化对象:

using Newtonsoft.Json;

var login = new LoginRequest
{
    Type = "login",
    condition = new Dictionary<string, string>[]

    {
        new Dictionary<string, string>()
        {
            {"key" , "email" },
            {"value", sUsername }
        },
        new Dictionary<string, string>()
        {
            {"key" , "password" },
            {"value", password }
        }
    }
};            
var jsonx = JsonConvert.SerializeObject(login);

enter image description here

请注意,使用JSON库比创建原始的JSON字符串更易于维护和操作。
希望这能对您有所帮助。

1

如果您想构建JSON,不需要使用Json.NET,您可以像这样使用匿名类型

var obj = new
{
    type = "login",
    condition = new Dictionary<string, object>()
    {
        { "0", new { key = "email", value = "myEmail" } },
        { "1", new { key = "password", value = "myPassword" } }
    }
};

string json = new JavaScriptSerializer().Serialize(obj);

0

你的答案是最简单的,但不要忘记在每个变量后面再次写上 @ 符号。 因此,字符串变成:

string json = @"
    {
        ""type"":""login"",
        ""condition"":{
            ""0"":{
                ""key"":""email"",
                ""value"":"+ sUsername +@"
            },
            ""1"":{
                ""key"":""password"",
                ""value"":"+ sPassword +@"
            }
        }
    }";

0

只需将变量添加到字符串中即可

string json = @"
        {
            ""type"":""login"",
            ""condition"":{
                ""0"":{
                    ""key"":""email"",
                    ""value"":"+ sUsername +"
                },
                ""1"":{
                    ""key"":""password"",
                    ""value"":"+ sPassword +"
                }
            }
        }";

你尝试过使用 'type' 替换 ""type"" 吗?这是我会采用的格式。 - Logan Young

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接