Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
           }
        }

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
  }]}

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]
Posted
Updated 18-Jul-11 1:31am
v2
Comments
komalilella 18-Jul-11 7:41am    
did u sent solution for that.iam nt getting any solution

You may try this C# JSON Library to deserialize JSON to dictionary.
 
Share this answer
 
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
 
Share this answer
 
v2

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