使用键值apir反序列化json字符串

编程入门 行业动态 更新时间:2024-10-18 08:36:31
本文介绍了使用键值apir反序列化json字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好. 实际上我的情况是这样的: 我拿了Windows应用程序.. 我通过使用我使用的服务有一个基于休息的json服务.我得到了json字符串(它有很多列).现在我通过使用键值对对json字符串进行了反序列化..我尝试过但不显示输出.. 像这样的代码: 使用系统; 使用System.Collections.Generic; 使用System.ComponentModel; 使用System.Data; 使用System.Drawing; 使用System.Linq; 使用System.Text; 使用System.Windows.Forms; 使用System.Net; 使用System.IO; 使用System.Web.Script.Serialization; 命名空间WindowsFormsApplication1 { 公共局部类Form1:Form { 公共Form1() { InitializeComponent(); } private void button1_Click(对象发送者,EventArgs e) { 字符串apiUrl ="api.geonames/citiesJSON?formatted=true&north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&用户名= demo& style = full; Uri地址=新Uri(apiUrl); //创建Web请求 System.Net.HttpWebRequest请求= System.Net.WebRequest.Create(地址)作为System.Net.HttpWebRequest; request.Method ="GET"; request.ContentType ="text/json"; 使用(System.Net.HttpWebResponse response = request.GetResponse()作为System.Net.HttpWebResponse) { //获取响应流 System.IO.StreamReader阅读器=新的System.IO.StreamReader(response.GetResponseStream()); 字符串strOutput = reader.ReadToEnd(); 字符串json = strOutput.ToString(); javaScriptSerializer js =新的JavaScriptSerializer(); var object1 = js.Deserialize< dictionary>< string,object>>(json); foreach(KeyValuePair< string,object>结果在object1中) { Console.WriteLine("key:{0},key:{1}",result.Key,result.Value); } } } } 但是未使用console.writeline.but如何在文本框中显示输出...如果有人知道请让我知道.. 在此先感谢.

Hi to all.. Actually my scenario like this: I took windows application.. I have one rest based json service by using that service i consumed.and i got json string(its is having so many columns are their).and now i deseralized that json string by using key value pair..i tried but not display output.. code like this: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.IO; using System.Web.Script.Serialization; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string apiUrl = "api.geonames/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); // Create the web request System.Net.HttpWebRequest request = System.Net.WebRequest.Create(address) as System.Net.HttpWebRequest; request.Method = "GET"; request.ContentType = "text/json"; using (System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse) { // Get the response stream System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream()); string strOutput = reader.ReadToEnd(); string json =strOutput.ToString(); javaScriptSerializer js = new JavaScriptSerializer(); var object1 = js.Deserialize<dictionary><string,object>>(json); foreach (KeyValuePair<string,object> result in object1) { Console.WriteLine("key:{0},key:{1}", result.Key, result.Value); } } } } but console.writeline is not used.but how display output in textbox...if any body knows pls let me know.. Thanks in Advance.

推荐答案

您的问题似乎就在这里: Your problem seems to lie here: object myobject=js.DeserializeObject(json);

此方法是否返回object?还是返回Dictionary<string,object>?如果它返回一个对象,则需要将该对象转换为Dictionary,以便可以使用ForEach迭代该集合.否则,您需要将方法的结果声明为Dictionary而不是对象. 希望对您有帮助

Does this method return an object? Or does it return a Dictionary<string,object>? If it returns an object, then you need to convert that object into a Dictionary, so that you can iterate the collection using ForEach. Otherwise you need to declare the result of the method as a Dictionary and not an object. Hope this helps

更多推荐

使用键值apir反序列化json字符串

本文发布于:2023-11-17 02:31:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608485.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   键值   序列化   apir   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!