如何使用键值对和字典反序列化json字符串. [英] how to deseralize a json string using keyvalue pair and dictionary..

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

问题描述

大家好.
我用一个Windows应用程序..在那个按钮和一个文本框中...
我写了这样的代码:

hi to all..
i take a windows application..in that one button and one textbox...
i wrote 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;
using System.Web.Script.Services;
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();
          textbox1.text= strOutput.ToString();
           }
        }


我调试上面的代码,那时候我喜欢在文本框中输入json字符串的按钮.像这样:


i debug the above code that time i cliked button that json string came in textbox. like this:

{"geonames": [  {    "fcodeName": "capital of a political entity",
    "countrycode": "MX",    "fcl": "P",    "fclName": "city, village,...",
    "name": "Mexico City",    "wikipedia": "",    "lng": -99.12766456604,
    "fcode": "PPLC",    "geonameId": 3530597,    "lat": 19.428472427036,
    "population": 12294193  },  {
    "fcodeName": "capital of a political entity",    "countrycode": "PH",
    "fcl": "P",    "fclName": "city, village,...",    "name": "Manila",
    "wikipedia": "",    "lng": 120.9822,    "fcode": "PPLC",
    "geonameId": 1701668,    "lat": 14.6042,    "population": 10444527  },  {
    "fcodeName": "capital of a political entity",    "countrycode": "BD",
    "fcl": "P",    "fclName": "city, village,...",    "name": "Dhaka",
    "wikipedia": "",    "lng": 90.40743827819824,    "fcode": "PPLC",
    "geonameId": 1185241,    "lat": 23.710395616597037,
    "population": 10356500  },  {
    "fcodeName": "capital of a political entity",    "countrycode": "KR",
    "fcl": "P",    "fclName": "city, village,...",    "name": "Seoul",
    "wikipedia": "",    "lng": 126.977834701538,    "fcode": "PPLC",
    "geonameId": 1835848,    "lat": 37.5682561388953,    "population": 10349312
  },  {    "fcodeName": "capital of a political entity",    "countrycode": "ID",
    "fcl": "P",    "fclName": "city, village,...",    "name": "Jakarta",
    "wikipedia": "",    "lng": 106.84513092041016,    "fcode": "PPLC",
    "geonameId": 1642911,    "lat": -6.214623197035775,    "population": 8540121
  },  {    "fcodeName": "capital of a political entity",    "countrycode": "JP",
    "fcl": "P",    "fclName": "city, village,...",    "name": "Tokyo",
    "wikipedia": "",    "lng": 139.581298828125,    "fcode": "PPLC",
    "geonameId": 1850147,    "lat": 35.6148836824544,    "population": 8336599
  },  {    "fcodeName": "capital of a political entity",    "countrycode": "TW",
    "fcl": "P",    "fclName": "city, village,...",    "name": "Taipei",
    "wikipedia": "",    "lng": 121.531846,    "fcode": "PPLC",
    "geonameId": 1668341,    "lat": 25.047763,    "population": 7871900  },  {
    "fcodeName": "capital of a political entity",    "countrycode": "CN",
    "fcl": "P",    "fclName": "city, village,...",    "name": "Beijing",
    "wikipedia": "",    "lng": 116.397228240967,    "fcode": "PPLC",
    "geonameId": 1816670,    "lat": 39.9074977414405,    "population": 7480601
  },  {    "fcodeName": "capital of a political entity",    "countrycode": "CO",
    "fcl": "P",    "fclName": "city, village,...",    "name": "Bogotá",
    "wikipedia": "",    "lng": -74.08175468444824,    "fcode": "PPLC",
    "geonameId": 3688689,    "lat": 4.609705849789108,    "population": 7102602
  },  {    "fcodeName": "capital of a political entity",    "countrycode": "HK",
    "fcl": "P",    "fclName": "city, village,...",    "name": "Hong Kong",
    "wikipedia": "",    "lng": 114.157691001892,    "fcode": "PPLC",
    "geonameId": 1819729,    "lat": 22.2855225817732,    "population": 7012738
  }]}


在我的文本框输出中带有特殊字符,例如方形框..如何删除那些特殊字符..
但是我的计划是使用键值对和字典来获得反序列化的输出..如何?
如果有人知道请让我知道..充分满足需求..

[edit]已添加代码块-OriginalGriff [/edit]


in my textbox output came with special characters like square boxes..how to remove those special characters..
but my scnario is using keyvaluepair and dictionary to get the deserialize output..how?
if any body knows pls let me know..do the need full..

[edit]Code block added - OriginalGriff[/edit]

推荐答案

您可以尝试以下 C#JSON库以将JSON反序列化为字典.
You may try this C# JSON Library to deserialize JSON to dictionary.




您可能会看Mehdi Gholam的最新文章和JSON工具,"FastJSON" [ ^ ],位于CodeProject上,因为他的序列化器和反序列化器以某些方式显式支持Dictionary.

我已经读过,NewtonSoft的JSON.NET确实支持字典,而.NET的内置JSON不支持.

我自己尚未在这三种JSON实现中的任何一种上对Dictionary进行过试验.

您可能必须转换"您现在拥有的WebRequest结果JSON.也许您可以使用RegEx过滤掉奇怪的字符...一旦您分析了这些字符是什么.

显然,您的JSON中有一些针对每个内容元素重复的字段".

您在此处显示的JSON看起来像一个JSON关联数组.

Bill,祝你好运
Hi,

You might look at the recent article, and tools for JSON, by Mehdi Gholam, "FastJSON"[^], here on CodeProject, since his serializer and deserializer explicitly support Dictionaries in some ways.

I have read that, NewtonSoft''s JSON.NET does support Dictionaries, while .NET''s built-in JSON does not.

I have not experimented, myself, with Dictionaries, in any of these three JSON implementations.

You''ll probably have to "transform" the WebRequest result JSON you have now; perhaps use a RegEx to filter out weird characters ... once you''ve analyzed what those characters are.

Obviously you have some "fields" in your JSON that are duplicated for every content element.

The JSON you show here looks like a JSON associative array.

good luck, Bill


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

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