Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please , help. I am trying to deserialize json string(
{"response":[259,{"body":"я тебе ","mid":17759,"uid":18537276,"from_id":18537276,"date":1345466897,"read_state":1,"out":1},{"body":"jdjdjdjd","mid":17736,"uid":18537276,"from_id":18537276,"date":1345400400,"read_state":1,"out":1,"attachment":{"type":"doc","doc":{"did":118620269,"owner_id":18537276,"title":"IMG_20120722_220434.jpg","size":475878,"ext":"jpg","url":"http:\/\/vk.com\/doc18537276_118620269?hash=59f1fe51a02a65519c&dl=3972a9297f3da6a20c","thumb":"http:\/\/cs4343.vkontakte.ru\/u18537276\/-3\/m_69a4a578da.jpg","access_key":"7a6cb6559b179dec16"}}
) etc.

Here is code:
C#
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Response_GetHistory));
Response_GetHistory gh = new Response_GetHistory();
gh = (Response_GetHistory)ser.ReadObject(stream);

here is class
C#
public class Response_GetHistory
{
public int s;
public Message[] response;
}
 
public class Message
{
 
public string mid;
public string uid;
public string date;
public string read_state;
public string title;
public string body;
public Attachments attachments;
public Message fwd_messages;
public string chat_id;
public string chat_active;
public string users_count;
public string admin_id;
public string deleted;
}

but deserialization failed;
I think becase of number 259..What should I change in my classes?
Posted
Updated 21-Aug-12 7:46am
v2
Comments
Nueman 21-Aug-12 11:32am    
Please use the Improve question link to edit your question and put the code in a code block. It will make your query more readable and more likely to get a response. Thanks.
felipekm 21-Aug-12 12:51pm    
Where is your ajax call?
Please show us your json contruction.

I think you need to decorate the class with [DataContract()] attribute and properties with [DataMember(Name="<fieldname_in_json_string>")] attribute to tell the DataContractSerialiser which properties of the class to map to which field in JSON. For e.g.

C#
[DataContract()]
public class Message
{
[DataMember(Name="mid")]
public string mid {get;set}
//so on for the all the others as well.


These attributes are in System.Runtime.Serialization namespace. Hope this helps.
 
Share this answer
 
Comments
dmitrij1990 21-Aug-12 12:01pm    
thanks, but how i can add atribute to field which has no name in json string?
[DataMember(Name="")] - generate exception
dmitrij1990 21-Aug-12 12:02pm    
I am about 259 at the begining of json string
I.explore.code 21-Aug-12 13:22pm    
well, the JSON should be well formed. If you control the construction of JSON then u can try naming that field. Not sure if its an index of an array of JSON objects. Even with other JSON deserialisers you would need to have that field identifiable by a string name.
Have you tried with other serializers (like fastJSON[^] or Json.NET[^])- just to see, if the result is the same? To me it looks like the 259 value is corresponding to the public int s; field, but without field name, and wrong position. Thy to change "s" to something longer, just to see...
Googleing revealed, that DataContractJsonSerialize has several bugs...
 
Share this answer
 
Your json seems to be invalid check it here : http://jsonlint.com/[^]
 
Share this answer
 
Comments
Matt T Heffron 21-Aug-12 17:06pm    
Other than the missing closing delimiters (i.e., "}]}" which were implied by the original poster) this passes jsonlint.com.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900