supa pati
supa pati

Reputation: 73

Getting variable data from string in c#

I have got data downloaded from url it is as follows.

//[
  {
    "id": "2932675",
    "t": "GNK",
    "e": "LON",
    "l": "915.00",
    "l_fix": "915.00",
    "l_cur": "GBX915.00",
    "s": "0",
    "ltt": "5:08PM GMT",
    "lt": "Dec 11,
    "5": 08PM
    "GMT",
    "
    "lt_dts": "2015-12-11T17:08:26Z",
    "c": "-7.50",
    "c_fix": "-7.50",
    "cp": "-0.81",
    "cp_fix": "-0.81",
    "ccol": "chr",
    "pcls_fix": "922.5"
  }
]

and want following variable t : GNK and l:915 from above string and done following

void method1()
   {
      string scrip = textBox1.Text;
      string s;
      WebClient wc = new WebClient();
      string url = ("http://finance.google.com/finance/infoclient=ig&q=NSE:" + scrip);
      s = wc.DownloadString(url);
      textBox2.Text = s.Substring(58, 6);
      textBox3.Text = s;

     }

        public class LatestPrice
    {
        public string id { get; set; }
        public string Name { get; set; }
        public string type { get; set; }
        public string l { get; set; }
        public string l_fix { get; set; }
        public string l_cur { get; set; }
        public string s { get; set; }
        public string lt { get; set; }
        public string lt_dts { get; set; }
        public string c { get; set; }
        public string c_fix { get; set; }
        public string cp { get; set; }
        public string cp_fix { get; set; }
        public string ccol { get; set; }
        public string pcls_fix { get; set; }
    }
    public string[][] convert_string()
    {
        string[][] stockprice = null;
        string stockprice1 = null;
        string getdownloadstr = getstring();

        stockprice1 = getdownloadstr.Replace("//", "").Trim();
        var v = JsonConvert.DeserializeObject<List<LatestPrice>>(stockprice1);
        }

i have made changes to program -- but how to access the t : gnk value or l = 915 value

Upvotes: 1

Views: 162

Answers (5)

supa pati
supa pati

Reputation: 73

i have done in follwing way

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Web;
using System.Timers;
using System.IO;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text.RegularExpressions;



namespace google_downloader
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            WebClient wc = new WebClient();
            string s= wc.DownloadString("http://finance.google.com/finance/info?client=ig&q=NSE:sbin");

            //index for t 
            int index7= s.IndexOf('t');
            int index8 = s.IndexOf('e');
            textBox1.Text = ("frist index is" + index7 +  "second indes is   " + index8);

            textBox1.Text = s.Substring(index7+6,(index8-index7)-10);





       }
    }
}

Upvotes: 0

SH7
SH7

Reputation: 752

1 . Add NewtonSoft.Json in your Reference in Solution Explorer. Steps(Reference [rightClick]-> Manage NuGetPackage -> Search for "Json.Net" -> Install them)

  1. Add using Newtonsoft.Json;

code :

public class CurrentValue
    {
        public string id { get; set; }
        public string Name { get; set; }
        public string type { get; set; }
        public string l { get; set; }
        public string l_fix { get; set; }

        public string l_cur { get; set; }
        public string s { get; set; }
        public string lt { get; set; }
        public string lt_dts { get; set; }
        public string c { get; set; }
        public string c_fix { get; set; }
        public string cp { get; set; }
        public string cp_fix { get; set; }
        public string ccol { get; set; }
        public string pcls_fix { get; set; }

    }

Create a method and use following code

 Uri url = new Uri("http://www.google.com/finance/info?q=NSE%3A" + NameofCompany);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.ContentType = "application/json; charset=utf-8";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string Responsecontent = new StreamReader(response.GetResponseStream()).ReadToEnd();
                string CurrentContent = Responsecontent.Replace("//", "").Trim();
                var v = JsonConvert.DeserializeObject<List<CurrentValue>>(CurrentContent);

Now "var v" have all the data of the class "CurrentValue".

You can load a xml file containing all " NameofCompany " and Load it on FormLoad. Use a for loop to get data of all " NameofCompany "

Upvotes: 0

TVOHM
TVOHM

Reputation: 2742

You could use Regex to match the data you need:

string t = Regex.Match(str, "\"t\" : \"[A-Z]{3}\"").Value;
string l = Regex.Match(str, "\"l\" : \"\\d{3}.\\d{2}\"").Value;

Where str is the data string you have downloaded.

String t matches a substring that is in the format "t" : "XXX", where XXX can contain any uppercase characters.

String l matches a substring that is in the format "l" : "XXX.XX", where XXX.XX can contain any digit.

Upvotes: 0

user1672994
user1672994

Reputation: 10839

You can convert to JArray to JObject and can get the value directly through JOject parameter key.

string jsonStr = "[{ \"id\":\"2932675\", \"t\" : \"GNK\" , \"e\" : \"LON\" , \"l\" : \"915.00\" , \"l_fix\" : \"915.00\" , \"l_cur\" : \"GBX915.00\" , \"s\": \"0\" , \"ltt\":\"5:08PM GMT\" , \"lt\" : \"Dec 11 5:08PM GMT\"}]";
var obj = JsonConvert.DeserializeObject<JArray>(jsonStr).ToObject<List<JObject>>().FirstOrDefault();

Console.WriteLine("t = " + obj["t"]);
Console.WriteLine("l = " + obj["l"]);

The above print the output as

t = GNK
l = 915.00

You can refer to this fiddle created for your question.

Upvotes: 2

Ken
Ken

Reputation: 2808

Parse it either split at , and then (split on : ) add each line to dictionary using the t as the key and the other as the value. Then you could simply access by t and also by l . Split the entire string by commas you have a list of item : value, then split on colon and add to dictionary. Then look up info in dictionary getvalue = Dictionary[key] ;

Upvotes: 1

Related Questions