Ok, d'abord écoutez-moi en regardant cette url:Impossible d'obtenir <pour apparaître dans la chaîne JSON
http://api.flickr.com/services/feeds/photos_public.gne?format=json
Remarquez comment la chaîne de description a html non codé en elle, comme < ,>, etc.
Voici comment je suis retourné mon JSON en utilisant ASP.NET 3.5 et C#:
context.Response.ContentType = "text/plain";
context.Response.Charset = Encoding.UTF8.ToString();
int i = 1;
List<Product> products = GetTestProducts();
List<CrImageList> imageList = new List<CrImageList>();
foreach(Product p in products)
{
string imageTag = HttpUtility.HtmlEncode(string.Format(@"<img src=""{0}"" alt="""">", ImageUrl(p.Image, false)));
imageList.Add(new CrImageList{ImageTag = imageTag});
i++;
}
string jsonString = imageList.ToJSON();
context.Response.Write(jsonString);
et voici le JSON retourné par ce code qui est appelé quand je salut t mon .ashx et appelle ce code dans la méthode qui contient:
[{"ImageTag":"<img src="http://www.ourdomain.com/image/540.jpg" alt="">"},{"ImageTag":"<img src="http://www.ourdomain.com/image/642.jpg" alt="">"}]
Maintenant, comment puis-je obtenir les caractères codés pour montrer réellement comme des caractères HTML dans la chaîne de texte retourné par mon JSON?
Je veux < pour montrer < et ainsi de suite comme Flickr était capable de faire dans leur JSON.
Si je prends le HtmlEncode:
string.Format (@ "", ImageUrl (p.Image, false));
Alors je commence à obtenir le/u00 dans ma chaîne:
[{"ImageTag":"\u003cimg src=\"http://www.example.com/cat_image/540.jpg\" alt=\"\"\u003e"},
...
Alors, pourquoi ne pas le JSON Flickr retour tout sauf html propre dans leur description?
Vous devez nous montrer ce que le toJSON méthode ne – Jab