使用JsonConvert.SerializeObject时如何仅获取值?我不需要重复的单词,例如id,name等。

例如:{id:189,姓名:“ Paul”,年龄:31,} x {[189,'Paul',31]}

谢谢!

我需要与PageList类一起使用

public class PageList {
    IEnumerable _rows;
    int _total;
    int _page;
    int _records;
    object _userData;

    public PageList(IEnumerable rows, int page, int total, int records, object userData) {
        _rows = rows;
        _page = page;
        _total = total;
        _records = records;
        _userData = userData;
    }

    public PageList(IEnumerable rows, int page, int total, int records)
        : this(rows, page, total, records, null) {

    }

    public int total { get { return _total; } }

    public int page { get { return _page; } }

    public int records { get { return _records; } }

    public IEnumerable rows { get { return _rows; } }

    [JsonIgnore]
    public object userData { get { return _userData; } }

    public override string ToString() {
        return Newtonsoft.Json.JsonConvert.SerializeObject(this, new IsoDateTimeConverter() { DateTimeFormat = "dd-MM-yyyy hh:mm:ss" });
    }
}

最佳答案

我能想到的最接近的是

var yourObjectList = List<YourObject>(){.....}

string s = JsonConvert.SerializeObject(GetObjectArray(yourObjectList));

public static IEnumerable<object> GetObjectArray<T>(IEnumerable<T> obj)
{
    return obj.Select(o => o.GetType().GetProperties().Select(p => p.GetValue(o, null)));
}

关于c# - Json.NET在C#中仅输出值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10541062/

10-12 05:42