1

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

5
  • 3
    You need a JSON decoder - Json.NET is very good. Commented Dec 12, 2015 at 8:23
  • 1
    Why do you allow the user to change your 'script'? Commented Dec 12, 2015 at 8:28
  • so that user can view different quotes other wise i have to write no of code for no of different quotes by measuring the string length Commented Dec 12, 2015 at 8:46
  • can please give example of json decoding Commented Dec 12, 2015 at 8:46
  • @supapati - Please see the answer for how to deserialize JSON. Commented Dec 12, 2015 at 8:56

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I got some error ----- A first chance exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll Additional information: Additional text encountered after finished reading JSON content: ]. Path '', line 21, position 1.
Is your Json not is correct format. Can you please check the created fiddle.
A first chance exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll Additional information: Additional text encountered after finished reading JSON content: ]. Path '', line 21, position 1. If there is a handler for this exception, the program may be safely continued.
Your Json does not contain array opening bracket [ but contains closing bracket ]. Can you please fix your JSON string so that program works flowless.
1

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

Comments

0

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.

Comments

0

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 "

3 Comments

code run but not output. i have defined textBox1.Text = CurrentValue.t
if i want to access of var v say price of stock so how to get
string Value = v[0].l_cur.ToString().Trim().Replace("Rs.", ""); The Replace is use if you need only the Value and not the 'Rs' before it.
0

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);





       }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.