Reputation: 5119
Im new to json and c# and need help in the right direction.
Look at the picture down below and you see the json string looking funny with slashes.
How can i remove the slashes or what parser should i use if this is wrong
This is my C# code:
PostList p = new PostList();
Posts posts1 = new Posts();
posts1.username = "aaaa";
posts1.message = "hej again";
posts1.time = "time1";
p.setPost(posts1);
Posts posts2 = new Posts();
posts2.username = "bbbb";
posts2.message = "hej again again";
posts2.time = "time2";
p.setPost(posts2);
Posts posts3 = new Posts();
posts3.username = "cccc";
posts3.message = "hej again again agin";
posts3.time = "time3";
p.setPost(posts3);
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(p.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, p);
string json = Encoding.Default.GetString(ms.ToArray());
This is my class that i try to serialize
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
namespace Project2
{
[DataContract]
public class PostList
{
[DataMember]
private List<Posts> posts { get; set; }
public PostList(){
this.posts = new List<Posts>();
}
public List<Posts> getPostContainterList()
{
return posts;
}
public void setPost(Posts post)
{
posts.Add(post);
}
}
[DataContract]
public class Posts
{
[DataMember]
public String message { get; set; }
[DataMember]
public String time { get; set; }
[DataMember]
public String username { get; set; }
}
}
UPDATE SHOWING WHERE STRING COME FROM
Upvotes: 1
Views: 847
Reputation: 6425
Hit your service with something like Fiddler / Chrome tools / IE f12 developer tools to see the raw response. I suspect that your JSON is actually fine, and that the slashes are added by the JSON viewer you use.
If ur new to JSON,a great quick resource for pasting in long JSON strings and seeing them hierarchically is jsonlint.com . It validates them too.
Upvotes: 2