Question : google search API C# jSON question

My typeof declaration

[Serializable]
      public class GooglePage
{
  public string start;
  public string label;
}
        [Serializable]
public class GoogleResponseCursor
{
  public GooglePage[] pages;
  public int estimatedResultCount;
  public int currentPageIndex;
  public string moreResultsUrl;
}
        [Serializable]
public class GoogleResponseData
{
  public string[] results;
  public GoogleResponseCursor cursor;
}
      [Serializable]
public class GoogleResponseStandard
{
  public GoogleResponseData responseData;
  public string responseDetails;
  public string responseStatus;
}
      

soaprequest is printed correctly and I have checked it. soaprequest is a string which contains the serialized json response from google api



MemoryStream ms = new MemoryStream();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(GoogleResponseStandard));        
ms = new MemoryStream(Encoding.Unicode.GetBytes(soaprequest));
        ser = new DataContractJsonSerializer(typeof(GoogleResponseStandard));
                    
        GoogleResponseStandard person2 = ser.ReadObject(ms) as GoogleResponseStandard;
                          ms.Close();        

I keep getting the error

End element 'item' from namespace '' expected. Found element 'GsearchResultClass' from namespace ''.

Answer : google search API C# jSON question

Test page :

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Runtime.Serialization.Json;
using System.Text;

namespace WebApplication21
{
    [Serializable]
    public class GooglePage
    {
        public string start;
        public string label;
    }
    
    [Serializable]
    public class GoogleResponseCursor
    {
        public GooglePage[] pages;
        public int estimatedResultCount;
        public int currentPageIndex;
        public string moreResultsUrl;
    }
    
    [Serializable]
    public class GoogleResponseData
    {
        public string[] results;
        public GoogleResponseCursor cursor;
    }
    
    [DataContract]
    public class ResponseData
    {
        [DataMember(Name = "responseData")]
        public ResultList responseData
        { get; set; }
        [DataMember(Name = "responseDetails")]
        public string responseDetails
        { get; set; }
        [DataMember(Name = "responseStatus")]
        public string responseStatus
        { get; set; }
    }

    [DataContract]
    public class ResultList
    {
        [DataMember(Name = "results")]
        public GsearchResultClass[] GResult
        { get; set; }
    }

    [DataContract]
    public class GsearchResultClass
    {
        [DataMember(Name = "GsearchResultClass")]
        public string GsearchResult
        { get; set; }
    }



    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string soaprequest = "{\"responseData\":{\"results\":[{\"GsearchResultClass\":\"GwebSearch\",\"unescapedUrl\":\"http://en.wikipedia.org/wiki/Paris_Hilton\",\"url\":\"http://en.wikipedia.org/wiki/Paris_Hilton\",\"visibleUrl\":\"en.wikipedia.org\",\"titleNoFormatting\":\"Paris Hilton - Wikipedia, the free encyclopedia\"},{\"GsearchResultClass\":\"GwebSearch\",\"unescapedUrl\":\"http://www.imdb.com/name/nm0385296/\",\"url\":\"http://www.imdb.com/name/nm0385296/\",\"visibleUrl\":\"www.imdb.com\",\"titleNoFormatting\":\"Paris Hilton\"}],\"cursor\":{\"pages\":[{\"start\":\"0\",\"label\":1}, {\"start\":\"4\",\"label\":2},{\"start\":\"8\",\"label\": 3 },{\"start\":\"12\",\"label\":4}],\"estimatedResultCount\":\"59600000\",\"currentPageIndex\": 0,\"moreResultsUrl\":\"http://www.google.com/search?oe\"}},\"responseDetails\": null,\"responseStatus\": 200}";

            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ResponseData));
            byte[] respBytes = ASCIIEncoding.UTF8.GetBytes(soaprequest);
            using (StreamReader reader = new StreamReader(new MemoryStream(respBytes)))
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ResponseData));
                ResponseData returnObj = (ResponseData)serializer.ReadObject(reader.BaseStream);
            }
        }
    }
}
Random Solutions  
 
programming4us programming4us