How do I read Json results from yahoo

M

Matthijs de Z

Hi,

I'm having difficulties reading a resultset that yahoo provides using
when you send this request:
http://d.yimg.com/autoc.finance.yah...llback=YAHOO.Finance.SymbolSuggest.ssCallback
(used in an example on http://stackoverflow.com/questions/885456/stock-ticker-symbol-lookup-api)

I've modified the result to a const string like this:
const string myTest = "{\"ResultSet\":{\"Query\":\"ya\",\"Result\":
[{\"symbol\":\"YHOO\",\"name\": \"Yahoo! Inc.\",\"exch\": \"NMS\",
\"type\": \"S\",\"exchDisp\":\"NASDAQ\"},{\"symbol\":\"AUY\",\"name\":
\"Yamana Gold, Inc.\",\"exch\": \"NYQ\",\"type\": \"S\",\"exchDisp\":
\"NYSE\"},{\"symbol\":\"YZC\",\"name\": \"Yanzhou Coal Mining Co. Ltd.
\",\"exch\": \"NYQ\",\"type\": \"S\",\"exchDisp\":\"NYSE\"},{\"symbol
\":\"YRI.TO\",\"name\": \"YAMANA GOLD INC COM NPV\",\"exch\": \"TOR\",
\"type\": \"S\",\"exchDisp\":\"Toronto\"},{\"symbol\":\"8046.TW\",
\"name\": \"NAN YA PRINTED CIR TWD10\",\"exch\": \"TAI\",\"type\": \"S
\",\"exchDisp\":\"Taiwan\"},{\"symbol\":\"600319.SS\",\"name\":
\"WEIFANG YAXING CHE 'A'CNY1\",\"exch\": \"SHH\",\"type\": \"S\",
\"exchDisp\":\"Shanghai\"},{\"symbol\":\"1991.HK\",\"name\": \"TA YANG
GROUP\",\"exch\": \"HKG\",\"type\": \"S\",\"exchDisp\":\"Hong Kong\"},
{\"symbol\":\"1303.TW\",\"name\": \"NAN YA PLASTIC TWD10\",\"exch\":
\"TAI\",\"type\": \"S\",\"exchDisp\":\"Taiwan\"},{\"symbol\":\"0294.HK
\",\"name\": \"YANGTZEKIANG\",\"exch\": \"HKG\",\"type\": \"S\",
\"exchDisp\":\"Hong Kong\"},{\"symbol\":\"YAVY\",\"name\": \"Yadkin
Valley Financial Corp.\",\"exch\": \"NMS\",\"type\": \"S\",\"exchDisp
\":\"NASDAQ\"}]}}";

How can I get the tickers and names in a dictionary (or some object
like that) so I can use it as a dataresource for a textfield
(autocomplete).

I've tried some linq, but it seems that there's no linq functionallity
to Json and I don't know how to get the data needed out of this
resultset.

Any help would be appreciated,.
 
A

Arne Vajhøj

Hi,

I'm having difficulties reading a resultset that yahoo provides using
when you send this request:
http://d.yimg.com/autoc.finance.yah...llback=YAHOO.Finance.SymbolSuggest.ssCallback
(used in an example on http://stackoverflow.com/questions/885456/stock-ticker-symbol-lookup-api)

I've modified the result to a const string like this:
const string myTest = "{\"ResultSet\":{\"Query\":\"ya\",\"Result\":
[{\"symbol\":\"YHOO\",\"name\": \"Yahoo! Inc.\",\"exch\": \"NMS\",
\"type\": \"S\",\"exchDisp\":\"NASDAQ\"},{\"symbol\":\"AUY\",\"name\":
\"Yamana Gold, Inc.\",\"exch\": \"NYQ\",\"type\": \"S\",\"exchDisp\":
\"NYSE\"},{\"symbol\":\"YZC\",\"name\": \"Yanzhou Coal Mining Co. Ltd.
\",\"exch\": \"NYQ\",\"type\": \"S\",\"exchDisp\":\"NYSE\"},{\"symbol
\":\"YRI.TO\",\"name\": \"YAMANA GOLD INC COM NPV\",\"exch\": \"TOR\",
\"type\": \"S\",\"exchDisp\":\"Toronto\"},{\"symbol\":\"8046.TW\",
\"name\": \"NAN YA PRINTED CIR TWD10\",\"exch\": \"TAI\",\"type\": \"S
\",\"exchDisp\":\"Taiwan\"},{\"symbol\":\"600319.SS\",\"name\":
\"WEIFANG YAXING CHE 'A'CNY1\",\"exch\": \"SHH\",\"type\": \"S\",
\"exchDisp\":\"Shanghai\"},{\"symbol\":\"1991.HK\",\"name\": \"TA YANG
GROUP\",\"exch\": \"HKG\",\"type\": \"S\",\"exchDisp\":\"Hong Kong\"},
{\"symbol\":\"1303.TW\",\"name\": \"NAN YA PLASTIC TWD10\",\"exch\":
\"TAI\",\"type\": \"S\",\"exchDisp\":\"Taiwan\"},{\"symbol\":\"0294.HK
\",\"name\": \"YANGTZEKIANG\",\"exch\": \"HKG\",\"type\": \"S\",
\"exchDisp\":\"Hong Kong\"},{\"symbol\":\"YAVY\",\"name\": \"Yadkin
Valley Financial Corp.\",\"exch\": \"NMS\",\"type\": \"S\",\"exchDisp
\":\"NASDAQ\"}]}}";

How can I get the tickers and names in a dictionary (or some object
like that) so I can use it as a dataresource for a textfield
(autocomplete).

I've tried some linq, but it seems that there's no linq functionallity
to Json and I don't know how to get the data needed out of this
resultset.

Any help would be appreciated,.

You should be able to write a data class and then use
JavaScriptSerializer Deserialize to get the data in
format that can easily be processed.

Arne
 
A

Arne Vajhøj

I'm having difficulties reading a resultset that yahoo provides using
when you send this request:
http://d.yimg.com/autoc.finance.yah...llback=YAHOO.Finance.SymbolSuggest.ssCallback

(used in an example on
http://stackoverflow.com/questions/885456/stock-ticker-symbol-lookup-api)

I've modified the result to a const string like this:
const string myTest = "{\"ResultSet\":{\"Query\":\"ya\",\"Result\":
[{\"symbol\":\"YHOO\",\"name\": \"Yahoo! Inc.\",\"exch\": \"NMS\",
\"type\": \"S\",\"exchDisp\":\"NASDAQ\"},{\"symbol\":\"AUY\",\"name\":
\"Yamana Gold, Inc.\",\"exch\": \"NYQ\",\"type\": \"S\",\"exchDisp\":
\"NYSE\"},{\"symbol\":\"YZC\",\"name\": \"Yanzhou Coal Mining Co. Ltd.
\",\"exch\": \"NYQ\",\"type\": \"S\",\"exchDisp\":\"NYSE\"},{\"symbol
\":\"YRI.TO\",\"name\": \"YAMANA GOLD INC COM NPV\",\"exch\": \"TOR\",
\"type\": \"S\",\"exchDisp\":\"Toronto\"},{\"symbol\":\"8046.TW\",
\"name\": \"NAN YA PRINTED CIR TWD10\",\"exch\": \"TAI\",\"type\": \"S
\",\"exchDisp\":\"Taiwan\"},{\"symbol\":\"600319.SS\",\"name\":
\"WEIFANG YAXING CHE 'A'CNY1\",\"exch\": \"SHH\",\"type\": \"S\",
\"exchDisp\":\"Shanghai\"},{\"symbol\":\"1991.HK\",\"name\": \"TA YANG
GROUP\",\"exch\": \"HKG\",\"type\": \"S\",\"exchDisp\":\"Hong Kong\"},
{\"symbol\":\"1303.TW\",\"name\": \"NAN YA PLASTIC TWD10\",\"exch\":
\"TAI\",\"type\": \"S\",\"exchDisp\":\"Taiwan\"},{\"symbol\":\"0294.HK
\",\"name\": \"YANGTZEKIANG\",\"exch\": \"HKG\",\"type\": \"S\",
\"exchDisp\":\"Hong Kong\"},{\"symbol\":\"YAVY\",\"name\": \"Yadkin
Valley Financial Corp.\",\"exch\": \"NMS\",\"type\": \"S\",\"exchDisp
\":\"NASDAQ\"}]}}";

How can I get the tickers and names in a dictionary (or some object
like that) so I can use it as a dataresource for a textfield
(autocomplete).

I've tried some linq, but it seems that there's no linq functionallity
to Json and I don't know how to get the data needed out of this
resultset.

Any help would be appreciated,.

You should be able to write a data class and then use
JavaScriptSerializer Deserialize to get the data in
format that can easily be processed.

Example code:

using System;
using System.Linq;
using System.Collections.Generic;
using System.Web.Script.Serialization;

namespace E
{
public class Entry
{
public string symbol { get; set; }
public string name { get; set; }
public string exch { get; set; }
public string type { get; set; }
public string exchDisp { get; set; }
}
public class ResultSetData
{
public string Query { get; set; }
public List<Entry> Result { get; set; }
}
public class Data
{
public ResultSetData ResultSet { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
string s = "{\"ResultSet\":{\"Query\":\"ya\",\"Result\":
[{\"symbol\":\"YHOO\",\"name\": \"Yahoo! Inc.\",\"exch\": \"NMS\",
\"type\": \"S\",\"exchDisp\":\"NASDAQ\"},{\"symbol\":\"AUY\",\"name\":
\"Yamana Gold, Inc.\",\"exch\": \"NYQ\",\"type\": \"S\",\"exchDisp\":
\"NYSE\"},{\"symbol\":\"YZC\",\"name\": \"Yanzhou Coal Mining Co. Ltd.
\",\"exch\": \"NYQ\",\"type\": \"S\",\"exchDisp\":\"NYSE\"},{\"symbol
\":\"YRI.TO\",\"name\": \"YAMANA GOLD INC COM NPV\",\"exch\": \"TOR\",
\"type\": \"S\",\"exchDisp\":\"Toronto\"},{\"symbol\":\"8046.TW\",
\"name\": \"NAN YA PRINTED CIR TWD10\",\"exch\": \"TAI\",\"type\": \"S
\",\"exchDisp\":\"Taiwan\"},{\"symbol\":\"600319.SS\",\"name\":
\"WEIFANG YAXING CHE 'A'CNY1\",\"exch\": \"SHH\",\"type\": \"S\",
\"exchDisp\":\"Shanghai\"},{\"symbol\":\"1991.HK\",\"name\": \"TA YANG
GROUP\",\"exch\": \"HKG\",\"type\": \"S\",\"exchDisp\":\"Hong Kong\"},
{\"symbol\":\"1303.TW\",\"name\": \"NAN YA PLASTIC TWD10\",\"exch\":
\"TAI\",\"type\": \"S\",\"exchDisp\":\"Taiwan\"},{\"symbol\":\"0294.HK
\",\"name\": \"YANGTZEKIANG\",\"exch\": \"HKG\",\"type\": \"S\",
\"exchDisp\":\"Hong Kong\"},{\"symbol\":\"YAVY\",\"name\": \"Yadkin
Valley Financial Corp.\",\"exch\": \"NMS\",\"type\": \"S\",\"exchDisp
\":\"NASDAQ\"}]}}";
Console.WriteLine(s);
JavaScriptSerializer jss = new JavaScriptSerializer();
Data d = jss.Deserialize<Data>(s);
foreach(var sym in d.ResultSet.Result.Where(r => r.exch ==
"HKG"))
{
Console.WriteLine(sym.name + " @ " + sym.exchDisp);
}
Console.ReadKey();
}
}
}

Arne
 
M

Matthijs de Z

On 21-06-2010 17:44, Arne Vajh j wrote:

[Arne's code]

Hi Arne,

Works great!!!
Thanks a lot for helping out.

Where did you get this knowledge from? Do you have / know a specific
tutorial somewhere on the web?
Kind regards,

Matthijs
 
A

Arne Vajhøj

On 21-06-2010 17:44, Arne Vajh j wrote:

[Arne's code]
Works great!!!
Where did you get this knowledge from? Do you have / know a specific
tutorial somewhere on the web?

I reused some code that I wrote for another problem in
another forum and then I looked for the class via
Google because someone in a third forum had told me
that .NET had builtin support for JSON.

Arne
 
M

Matthijs de Z

I reused some code that I wrote for another problem in
another forum and then I looked for the class via
Google because someone in a third forum had told me
that .NET had builtin support for JSON.

ok, well. That's new. I thought I needed the Json clas from
sourceforge.
But thanks again. Still happy with the code.
regards,

Matthijs
 
Top