使用键值对反序列化json字符串会导致错误 [英] Deseralize a json string using keyvalue pair causes error

查看:67
本文介绍了使用键值对反序列化json字符串会导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.当我使用键值对反序列化json字符串时.
像这样的代码:

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 = "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();
}
}</dictionary>






我遇到这样的错误:






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

************** 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 system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

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.



如果有人知道该解决方案,请告诉我..
提前想一想..


[edit]主题,标签,删除的喊话-OriginalGriff [/edit]



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


[edit]Subject, tags, SHOUTING removed - OriginalGriff[/edit]

推荐答案

所以它抱怨的行是:
So the line it is complaining about is:
var object1 = js.Deserialize<string,object>>(strResponse);

我认为这只是打错了字样,而>>"实际上是>"?

您需要查看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.


我以前遇到过此问题.使用JSON时,它将作为对象数组传输,并且不了解如何反序列化为字典.试试这个.
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>;


嗨.我想这串代码在逻辑上可能是错误的:
Hi. I suppose this string of code could be logically wrong:
var object1 = js.Deserialize<string,object>>(strResponse);


您需要:
1.从响应中获取响应主体流;
2.通过缓冲区从其中读取数据;
3.关闭请求\响应;
4.通过编码将缓冲区数据转换为字符串;
5.从字符串执行反序列化;


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;


这篇关于使用键值对反序列化json字符串会导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆