使用JsonWriter和动态类列表

时间:2014-09-22 08:45:21

标签: c# json json.net

我目前有一个单独的类列表构建器:

public class psuedoMe {
    public string relName { get; set; }
    public List<string> lstName { get; set; }
}

我有一个填充此功能的函数,但后来使用Json将其写入Newtonsoft.Json.JsonWriter

private static string returnJson(List<psuedoMe> sentList)
{
    StringBuilder jsonSB = new StringBuilder();
    StringWriter jsonSW = new StringWriter(jsonSB);
    using (JsonWriter jsonWrite = new JsonTextWriter(jsonSW))
    {
        jsonWrite.WriteStartArray();
        foreach (psuedoMe sentItem in sentList)
        {
            jsonWrite.WriteStartObject();
            foreach (System.Reflection.PropertyInfo propInfo in sentItem.GetType().GetProperties())
            {
                jsonWrite.WritePropertyName(propInfo.Name);
                jsonWrite.WriteValue(propInfo.GetValue(sentItem, null));
            }
            jsonWrite.WriteEndObject();
        }
        jsonWrite.WriteEndArray();
    }
    return jsonSB.ToString();
}

但是,当我尝试将public List<string> lstName写入jsonSB时,我收到错误消息。我尝试仅排除lstName中的JsonWriter,但这只会在列表中循环,并且最后不会将其写入jsonSB

是否仍然使用上述returnJson写入strings列表?

我得到的错误是:Newtonsoft.Json.JsonWriterException: Unsupported type: System.Collections.Generic.List1[System.String].

1 个答案:

答案 0 :(得分:3)

您使用的是JSON.NET的哪个版本?这是source code of JsonWriter class's WriteValue method

public virtual void WriteValue(object value)
    {
      if (value == null)
      {
        WriteNull();
        return;
      }
      else if (value is IConvertible)
      {
        IConvertible convertible = value as IConvertible;

        switch (convertible.GetTypeCode())
        {
          case TypeCode.String:
            WriteValue(convertible.ToString(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Char:
            WriteValue(convertible.ToChar(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Boolean:
            WriteValue(convertible.ToBoolean(CultureInfo.InvariantCulture));
            return;
          case TypeCode.SByte:
            WriteValue(convertible.ToSByte(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Int16:
            WriteValue(convertible.ToInt16(CultureInfo.InvariantCulture));
            return;
          case TypeCode.UInt16:
            WriteValue(convertible.ToUInt16(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Int32:
            WriteValue(convertible.ToInt32(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Byte:
            WriteValue(convertible.ToByte(CultureInfo.InvariantCulture));
            return;
          case TypeCode.UInt32:
            WriteValue(convertible.ToUInt32(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Int64:
            WriteValue(convertible.ToInt64(CultureInfo.InvariantCulture));
            return;
          case TypeCode.UInt64:
            WriteValue(convertible.ToUInt64(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Single:
            WriteValue(convertible.ToSingle(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Double:
            WriteValue(convertible.ToDouble(CultureInfo.InvariantCulture));
            return;
          case TypeCode.DateTime:
            WriteValue(convertible.ToDateTime(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Decimal:
            WriteValue(convertible.ToDecimal(CultureInfo.InvariantCulture));
            return;
          case TypeCode.DBNull:
            WriteNull();
            return;
        }
      }
#if !PocketPC && !NET20
      else if (value is DateTimeOffset)
      {
        WriteValue((DateTimeOffset)value);
        return;
      }
#endif
      else if (value is byte[])
      {
        WriteValue((byte[])value);
        return;
      }

      throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
    }

正如您所看到的,它并不支持List<T>。因此,对于List<T>,它将抛出Unsupported type exception,即使在异常消息中,它也建议使用JsonSerializer来获取对象的JSON表示。