Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi to all.when i deseralize a json string using keyvalue pair .
code like this:

private void button1_Click(object sender, EventArgs e)
string apiUrl = "";
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();

I got error like this:

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.ArgumentException: Invalid JSON primitive: System.Net.HttpWebResponse.
   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()
   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)
   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)
   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)
   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)
   at WindowsFormsApplication1.Form1.button1_Click(Object sender, EventArgs e) in D:\SK\consume a webservice\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 45
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

************** Loaded Assemblies **************
    Assembly Version:
    Win32 Version: 2.0.50727.3623 (GDR.050727-3600)
    CodeBase: file:///E:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
    Assembly Version:
    Win32 Version:
    CodeBase: file:///D:/SK/consume%20a%20webservice/WindowsFormsApplication1/WindowsFormsApplication1/bin/Debug/WindowsFormsApplication1.exe
    Assembly Version:
    Win32 Version: 2.0.50727.3623 (GDR.050727-3600)
    CodeBase: file:///E:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/
    Assembly Version:
    Win32 Version: 2.0.50727.3620 (GDR.050727-3600)
    CodeBase: file:///E:/WINDOWS/assembly/GAC_MSIL/System/
    Assembly Version:
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///E:/WINDOWS/assembly/GAC_MSIL/System.Drawing/
    Assembly Version:
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///E:/WINDOWS/assembly/GAC_MSIL/System.Configuration/
    Assembly Version:
    Win32 Version: 2.0.50727.3082 (QFE.050727-3000)
    CodeBase: file:///E:/WINDOWS/assembly/GAC_MSIL/System.Xml/
    Assembly Version:
    Win32 Version: 3.5.30729.3644
    CodeBase: file:///E:/WINDOWS/assembly/GAC_MSIL/System.Web.Extensions/
    Assembly Version:
    Win32 Version: 3.5.30729.1
    CodeBase: file:///E:/WINDOWS/assembly/GAC_MSIL/System.Web.Abstractions/
    Assembly Version:
    Win32 Version: 2.0.50727.3618 (GDR.050727-3600)
    CodeBase: file:///E:/WINDOWS/assembly/GAC_32/System.Web/
    Assembly Version:
    Win32 Version: 3.5.30729.1 built by: SP
    CodeBase: file:///E:/WINDOWS/assembly/GAC_MSIL/System.Core/

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the section.
The application must also be compiled with debugging

For example:

    < jitDebugging="true" />

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

If any body knows the solution for this pls let me know..
Thnaks in advance..

[edit]Subject, tags, SHOUTING removed - OriginalGriff[/edit]
Updated 21-Jul-11 23:14pm
OriginalGriff 22-Jul-11 3:25am    
1) DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.
2) Show us the code, rather than just a big - and fairly useless - dump of the error message. We need to see the code it occurred on, the code that generated the JSON string (if applicable) and a sample of the JSON that causes the problem. Use the "Improve question" widget to edit your question and provide better information.

So the line it is complaining about is:
var object1 = js.Deserialize<string,object>>(strResponse);
Which I assume is just mistyped and the ">>" is actually ">"?

You need to see what the strResponse data looks like - try writing it into a file or log or something: since the error is explicitly complaining about
System.ArgumentException: Invalid JSON primitive: System.Net.HttpWebResponse.
   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()
it looks to be a data error of some form.

If you let me know, it may be possible to work out what the error actually is.
Share this answer
I've encountered this problem before. When using JSON is it transfered as a object array and doesn't understand how deserialize into a dictionary. Try this.
Dictionary<string, object> values = ((object[])data)[0] as Dictionary<string, object>;
Share this answer
komalilella 5-Aug-11 1:08am    
hi mark nischailke..
thanks for replying...i got the solution 1 week back..i declared like that only..
komalilella 5-Aug-11 1:12am    
hi mark nischailke..
now i have one more doubt..on json..
now i have folder structure like this: i have sample folder in 'D' and in that sample one more hello folder and one text file like file.txt and again in that hello folder two text files like text.txt,bye.txt files are their. now my task is how to convert the entrire folder structure as a string... if u know any solution for this pls let me know..
thanks for advance...
[no name] 5-Aug-11 8:02am    
What does this have to do with JSON? What do you mean 'convert the entrire folder structure as a string"?
Hi. I suppose this string of code could be logically wrong:
var object1 = js.Deserialize<string,object>>(strResponse);

You need:
1. Get response body stream from response;
2. Read data from it though buffer;
3. Close request\response;
4. Convert buffer data to string though Encoding;
5. Perform deserialization from string;
Share this answer
[no name] 4-Aug-11 10:17am    
What a bloody useless answer. You have not addressed the OPs actual problem only restated the process already employed

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