本文介绍了使用键值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 ="http://api.geonames.org/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 = "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);

// 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.

推荐答案

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字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 03:58