【问题标题】:asp.net parse the array json using json.netasp.net 使用 json.net 解析数组 json
【发布时间】:2014-08-09 05:49:53
【问题描述】:

我写了这段代码:

public void ProcessRequest(HttpContext context)
{
    string m_order;

    try
    {
        var jsonSerilizer = new JavaScriptSerializer();
        var jsonString = String.Empty;
        context.Request.InputStream.Position = 0;
        using (var inputStream = new StreamReader(context.Request.InputStream))
        {
            jsonString = inputStream.ReadToEnd();
        }

        List<myClass> tmp = JsonConvert.DeserializeObject<List<myClass>>(jsonString);

        for (int i = 0; i < tmp.Count(); i++)
        {
            File.AppendAllText(@"d:\status\LOL.txt", "tmp["+(i+1)+"].rid=" +tmp[i].r_id  + "\r\n", Encoding.UTF8);

        }

myClass 定义为:

public class myClass
{
    public int f_id{get; set; }
    public int r_id { get; set; }
    public int count { get; set; }
    public int c_id { get; set; }
}

当我将 JSON 字符串从客户端发送到我的服务器时,我的服务器会转到 HTTP 处理程序并返回以下 JSON 字符串,一切都符合预期。

[
    {
        f_id:100,
        r_id:200,
        count:2,
        c_id=111
    },
    {
        f_id:120,
        r_id:200,
        count:1,
        c_id=111
    }
]

但是当程序到达这一行时:

List<myClass> tmp = JsonConvert.DeserializeObject<List<myClass>>(jsonString);

我的服务器崩溃并出现以下异常。

Newtonsoft.Json.JsonReaderException: Invalid JavaScript property identifier character: =. Path '[0].count', line 2, position 32.
   at Newtonsoft.Json.JsonTextReader.ParseUnquotedProperty()
   at Newtonsoft.Json.JsonTextReader.ParseProperty()
   at Newtonsoft.Json.JsonTextReader.ParseObject()
   at Newtonsoft.Json.JsonTextReader.ReadInternal()
   at Newtonsoft.Json.JsonTextReader.Read()
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
   at Portal.Get_O.ProcessRequest(HttpContext context)

【问题讨论】:

  • 看起来您的服务器正在打印出c_id=111,它应该打印出c_id:111。在不知道您的服务器代码的情况下,很难确切说明为什么会发生这种情况,但我觉得您在那里没有使用 JSON.net,所以您可能需要调查一下。
  • @MatthewHaugen 感谢我的朋友关注我的问题

标签: asp.net json json.net


【解决方案1】:

json "c_id=111" 字符串无效。正确的 json 应该始终在 key:value 对中。键值应以冒号 (:) 分隔。 { f_id:100, r_id:200, 计数:2, c_id=111 }, { f_id:120, r_id:200, 计数:1, c_id=111 }

正确的 json 应该如下所示: { f_id:100, r_id:200, 计数:2, c_id:111 }, { f_id:120, r_id:200, 计数:1, c_id:111 }

所以,只需验证“context.Request.InputStream”中包含的数据即可。分析这个问题可能会有所帮助。

【讨论】:

  • 键也需要用JSON引用。
猜你喜欢
  • 2013-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多