Click here to Skip to main content
16,011,702 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to all.

Actually I did like this:
I took rest based json service then I consumed and immediately de-serialized that json string.

I wrote the code like this:
C#
private void button1_Click(object sender, EventArgs e)
{
    string apiUrl = "http://api.geonames.org/citiesJSON?formatted=true&north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo&style=full";
    Uri address = new Uri(apiUrl);

    System.Net.HttpWebRequest Request = System.Net.WebRequest.Create(address) as System.Net.HttpWebRequest;
    Request.Method = "GET";
    Request.ContentType = "text/json";
     strResponse = Request.GetResponse().ToString();
    
    JavaScriptSerializer js = new JavaScriptSerializer();

    var object1 = js.Deserialize<dictionary><string,object>>(strResponse);

    foreach (KeyValuePair<string,object> result in object1)
        {

            textBox1.Text = textBox1.Text + result.Key;
            textBox2.Text = textBox2.Text + result.Value.ToString();

        }
 }

When I pressed CTRL+F5 that time I got error like this:
Invalid JSON primitive: System.Net.HttpWebResponse.

Please let me know the solution.

Thanks in advance.
Posted
Updated 12-Mar-18 20:21pm
v4

Your mistake is this line:

C++
string strres1 = strResponse.ToString();


After this your variable strres1 is equal to System.Net.HttpWebResponse. This is not a valid JSON string. This happens because the default behaviour of ToString inherited from object is to output the fully qualified type name. It is also an abomination to prefix a variable name of a type that is definitely not string with str. This made you think you had a string when it was really a HttpWebResponse object.
You probably meant to fetch the content from the response stream. The stream is fetched via strResponse.GetResponseStream method. Once you've got the stream you can read the content from there. That's the way it is done so please stick to that.

Best Regards,

—MRB
 
Share this answer
 
v2
Comments
komalilella 22-Jul-11 1:57am    
i removed this:
string strres1 = strResponse.ToString();
but stream reader would be used in xml write..but here iam using json..
iam getting same error.. invalid json primitive..
Manfred Rudolf Bihy 22-Jul-11 8:25am    
You'll have to read from the response stream and gather that into a string variable. This can be fed to the JSON deserialization method.
// a valid JSON object
var dataToLog = {'foo':'foovalue', 'bar':'barvalue'};

$.ajax({
type: 'POST',
url: '/logger/log',
contentType: 'application/json; charset=UTF-8',
data: JSON.stringify(dataToLog)
}).done(() => {
// do something
});
 
Share this answer
 
Comments
CHill60 13-Mar-18 8:30am    
The question is over 6 years old! Stick to answering new posts where the OP still needs help
Richard Deeming 13-Mar-18 10:51am    
Six years too late, and not even an attempt to answer the question!

As well as sticking to newer posts, you need to READ (and understand) THE QUESTION before posting a solution.

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