2

I write this code:

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 is defined as:

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; }
}

When I send the JSON string from the client to my server, my server goes to the HTTP Handler and returns the following JSON string, all as expected.

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

But when program hits this line:

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

my server crashes with the following exception.

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)
2
  • 1
    It looks like your server is printing out c_id=111 where it should be printing c_id:111. It's hard to say exactly why that's happening without knowing your server code, but I get the impression you aren't using JSON.net there, so you might want to look into that. Commented Aug 9, 2014 at 6:30
  • @MatthewHaugen thanks my friend for pay attention to my problem Commented Aug 9, 2014 at 7:01

1 Answer 1

1

The json "c_id=111" string is invalid. The correct json should always be in key:value pair. Key value should be separated by colon (:). { f_id:100, r_id:200, count:2, c_id=111 }, { f_id:120, r_id:200, count:1, c_id=111 }

The correct json should look like this: { f_id:100, r_id:200, count:2, c_id:111 }, { f_id:120, r_id:200, count:1, c_id:111 }

So, just verify the data contained in "context.Request.InputStream". It might be helpful to analyze this issue.

1
  • Keys also need to be quoted in JSON. Commented Aug 9, 2014 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.