Używam plugin jQuery, które potrzebują obiektu JSON z następującą strukturę (będę pobierania wartości z bazy danych):Jak powrócić JSON obiekt
{ results: [
{ id: "1", value: "ABC", info: "ABC" },
{ id: "2", value: "JKL", info: "JKL" },
{ id: "3", value: "XYZ", info: "XYZ" }
] }
Oto moja klasa:
public class results
{
int _id;
string _value;
string _info;
public int id
{
get
{
return _id;
}
set
{
_id = value;
}
}
public string value
{
get
{
return _value;
}
set
{
_value = value;
}
}
public string info
{
get
{
return _info;
}
set
{
_info = value;
}
}
}
to jest droga ja szeregować go:
results result = new results();
result.id = 1;
result.value = "ABC";
result.info = "ABC";
string json = JsonConvert.SerializeObject(result);
Ale to powróci tylko jeden wiersz. Czy możesz mi pomóc w zwrocie więcej niż jednego wyniku? Jak mogę uzyskać wynik w formacie określonym powyżej?
zmiennej result' 'to pojedynczy egzemplarz, a nie zbiór. Potrzebujesz kolekcji, jeśli chcesz mieć szereg przedmiotów. – Oded
możesz obejrzeć ten link: http://stackoverflow.com/questions/6201529/turn-c-sharp-object-into-a-json-string-in-net-4/6201609#6201609 – nercan
http://james.newtonking.com/projects/json/help/index.html?topic=html/SerializingCollections.htm –