Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.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

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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.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_Web Services - Fatal编程技术网

C# 在JSON对象中添加键和值

C# 在JSON对象中添加键和值,c#,json,web-services,C#,Json,Web Services,我对JSON非常陌生,我正在尝试在c#中创建一个web服务(.asmx),它将返回带有键和值的JSON对象。我正在为我的一个android应用程序创建此web服务,这就是为什么它必须返回带有键的JSON对象及其值的原因。以下是我的网络服务代码: using System; using System.Collections; using System.Linq; using System.Text; using System.Json; using System.Web.Services; usi

我对JSON非常陌生,我正在尝试在c#中创建一个web服务(.asmx),它将返回带有键和值的JSON对象。我正在为我的一个android应用程序创建此web服务,这就是为什么它必须返回带有键的JSON对象及其值的原因。以下是我的网络服务代码:

using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Json;
using System.Web.Services;
using System.Runtime.Serialization.Json;
using System.Web.Script.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.IO;
using System.Web.Script.Services;
using System.Collections.Generic;

namespace WebServiceExample
{
    /// <summary>
    /// Summary description for AddTwoNumbers
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    //[System.Web.Script.Services.ScriptService]
    public class AddTwoNumbers : System.Web.Services.WebService
    {

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string Add(int a, int b)
        {
            // 1st way to return Key and Value
            JsonObject jso = new JsonObject();
            JsonValue jv1 = 1;
            JsonValue jv2 = 2;
            jso.Add("Key-1", jv1.ToString());
            jso.Add("BoolValue", jv2.ToString());
            JavaScriptSerializer js = new JavaScriptSerializer();
            string strJSON = js.Serialize(jso);
            return strJSON;

        }

    }
}
使用系统;
使用系统集合;
使用System.Linq;
使用系统文本;
使用System.Json;
使用System.Web.Services;
使用System.Runtime.Serialization.Json;
使用System.Web.Script.Serialization;
使用System.ServiceModel;
使用System.ServiceModel.Web;
使用System.IO;
使用System.Web.Script.Services;
使用System.Collections.Generic;
命名空间WebService示例
{
/// 
///AddTwoNumber的摘要说明
/// 
[WebService(命名空间=”http://tempuri.org/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
//要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。
//[System.Web.Script.Services.ScriptService]
公共类addTwoNumber:System.Web.Services.WebService
{
[网络方法]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
公共字符串添加(整数a、整数b)
{
//返回键和值的第一种方法
JsonObject jso=新的JsonObject();
JsonValue jv1=1;
JsonValue jv2=2;
jso.Add(“Key-1”,jv1.ToString());
Add(“BoolValue”,jv2.ToString());
JavaScriptSerializer js=新的JavaScriptSerializer();
字符串strJSON=js.Serialize(jso);
返回strJSON;
}
}
}
通过以上代码,我得到以下输出:

请点击上面的链接查看我的输出

正如你所看到的,我得到的是关键,但不是价值。我被卡住了

请帮忙。谢谢

编辑:如果我用了错误的方法,请建议我在JSON对象中添加键和值的正确方法。我在谷歌上搜索了很多,但无法正确理解

请提供任何快速帮助

更新:

 public string GetPeople()
        {
            Dictionary<string, string> dict = new Dictionary<string, string>();


            dict.Add("Key-1", "value-1");
            dict.Add("Key-2", "value-2");
            dict.Add("Key-3", "value-3");
            JavaScriptSerializer js = new JavaScriptSerializer();
            string strJSON = js.Serialize(dict);
            return strJSON;
        }
公共字符串GetPeople()
{
Dictionary dict=新字典();
添加(“键1”、“值1”);
添加(“键2”、“值2”);
添加(“键3”、“值3”);
JavaScriptSerializer js=新的JavaScriptSerializer();
字符串strJSON=js.Serialize(dict);
返回strJSON;
}

创建字典。。。序列化字典。@Phill:我已经更新了代码。你能检查一下,让我知道我做的是否正确吗。