How to convert list to json
You are not saving the return of the Serialize result:
JsonConvert.Serialize(airport.getListItems());
return JsonConvert;
Corrected with correct return type. Converting from object to JSON produces a single string encoded as a JSON array, so your return type is string
, not List<string>
public string getListItems() {
AirportClass airport = new AirportClass();
//removed JsonConvert declaration cause that would hide the JsonConvert class, don't name variables the same name as classes
string result = JsonConvert.SerializeObject(airport.getListItems());
return result;
}
It does not produce a List, to do that you'd need to serialize each item seperately, which is different from serializing a list. In that case you'd just use a loop to call Serialize on each item.
user3723240
Updated on June 04, 2022Comments
-
user3723240 almost 2 years
I have a function that connects to the database and executes a query and returns the data, now I am trying to call that function and convert the data to JSON
public List<string> getListItems() { AirportClass airport = new AirportClass(); return JArray.Parse(airport.getListItems()); return airport.getListItems(); }
I keep getting this error 'The best overloaded method match for 'Newtonsoft.Json.Linq.JArray.Parse(string)' has some invalid arguments'
What am I doing wrong?
I have also tried the following:
public List<string> getListItems() { AirportClass airport = new AirportClass(); JavaScriptSerializer JsonConvert = new JavaScriptSerializer(); JsonConvert.Serialize(airport.getListItems()); return JsonConvert; }
but this also gives me an error 'Cannot implicitly convert type 'JavaScriptSerializer' to Generic.List'
I also tried:
public List<string> getListItems() { AirportClass airport = new AirportClass(); JsonSerializer JsonConvert = new JsonSerializer(); JsonConvert.Serialize(getListItems()); return airport.getListItems(); }
but this gives me another error 'No overload for method 'Serialize' takes 1 arguments'
-
Giannis Paraskevopoulos over 9 yearsFirst of all there is no point in two returns.
-
Ehsan Sajjad over 9 yearsyou are passing
JArray.parse()
list of string while it acceptsstring
-
AaronLS over 9 yearsParse is the wrong method to use here. Parsing is for converting JSON to objects. You are trying to convert an object to JSON. You need to Serialize: james.newtonking.com/json/help/index.html#
-
user3723240 over 9 yearsI still dont understand
-
AaronLS over 9 years@user3723240 Sorry my link was wrong, see this: james.newtonking.com/json/help/index.html?topic=html/…
-
-
user3723240 over 9 yearsThis gives me another error 'No overload for method 'Serialize' takes 1 arguments'
-
user3723240 over 9 years
JsonSerializer JsonConvert = new JsonSerializer(); string result = JsonConvert.Serialize(airport.getListItems()); return result;
-
AaronLS over 9 yearsAh i copied from your code, should be SerializeObject from the documentation. Also you shouldn't be declaring a variable named JsonConvert, there is already a class named that in the framework.
-
user3723240 over 9 yearsThat got rid of one error, now I am getting a new one for return result - 'Cannot implicitly convert type 'string' to 'System.Collections.Generic.List<string>'
-
AaronLS over 9 years@user3723240 Can you update your question to show an example of the JSON string you are trying to produce? Such as in the documentation they show the result
["Starcraft","Halo","Legend of Zelda"]
. Serializing to JSON produces a single string, but you are expecting a list of strings, so it is unclear if you want to serialize each object individually. You will need to take a step back and figure out what it is you are trying to accomplish.