Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd the key and value in the JSON object
    primarykey
    data
    text
    <p>I am really new in <strong>JSON</strong>, and I am trying to create a web service(<strong>.asmx</strong>) in <strong>c#</strong> that will return the JSON object with key and value. I am creating this web service for one of my <strong>android app</strong> that why its must to return the <strong>JSON</strong> object with <strong>key</strong> and its <strong>value</strong>. Below is my code of web sevice:</p> <pre><code>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 { /// &lt;summary&gt; /// Summary description for AddTwoNumbers /// &lt;/summary&gt; [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; } } } </code></pre> <p>With the above code I am getting the following output:</p> <p><a href="http://d.pr/i/NWxp" rel="nofollow">http://d.pr/i/NWxp</a></p> <p>Please click on the above link to see my output.</p> <p>As you can see I am getting the key but not the value. I am stuck.</p> <p>Please help. Thanks</p> <p><strong>EDIT :</strong> If I am doing in a wrong way, so please suggest me the right way to add keys and values in <strong>JSON</strong> object. I searched a lot on google but unable to understand properly.</p> <p><strong>ANY QUICK HELP PLEASE</strong></p> <p><strong>UPDATE :</strong></p> <pre><code> public string GetPeople() { Dictionary&lt;string, string&gt; dict = new Dictionary&lt;string, string&gt;(); 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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload