2
Answers

jarray with json.net

tarun janveja

tarun janveja

7y
280
1
hi,
 
i have below id from my table .i want to send resonponse with id in below format using json.net.how should i do.please help me.
 
id =qi,q3
 
format required = {"id":["qi","q3"]}
Answers (2)
0
tarun janveja

tarun janveja

NA 156 7.2k 7y
Rajeev it will be very specific ,it works for static keys.but if i got more than 2 matching values from my database as per the query .than what should i do.i think jarray works for me.but i am unable to do the same.please help me. i have save result from my query using dataready in one variable i.e data field.but how should i put result in jarray  and give final output
 
data =qi,q3,123,q4,.............
 
final output = {"id":["qi","q3","123","q4",...]}
 
0
Rajeev Punhani

Rajeev Punhani

NA 4.7k 402.7k 7y
Hi Tarun,
 
Please try the below code.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using Newtonsoft.Json;  
  4.                       
  5. public class Program  
  6. {  
  7.     public static void Main()  
  8.     {  
  9.         RootObject r=new RootObject();  
  10.         List<string> s=new List<string>();  
  11.         s.Add("q1") ;  
  12.           
  13.         s.Add("q2") ;  
  14.         r.id=s;  
  15.           
  16.    
  17.  string output = JsonConvert.SerializeObject(r);  
  18.           
  19.           
  20.         Console.WriteLine(output);  
  21.     }  
  22.       
  23.   
  24. }  
  25.     public class RootObject  
  26. {  
  27.     public List<string> id { getset; }  
  28. }