-8

I am getting the company stock information from a JSON Object by accessing the web method. The JSON object as shown below.

<StockQuotes>
         <Stock>
            <Symbol>CAT</Symbol>
            <Last>80.95</Last>
            <Date>7/20/2012</Date>
            <Time>4:01pm</Time>
            <Change>0.00</Change>
            <Open>N/A</Open>
            <High>N/A</High>
            <Low>N/A</Low>
            <Volume>0</Volume>
            <MktCap>52.807B</MktCap>
            <PreviousClose>80.95</PreviousClose>
            <PercentageChange>0.00%</PercentageChange>
            <AnnRange>67.54 - 116.95</AnnRange>
            <Earns>7.932</Earns>
            <P-E>10.21</P-E>
            <Name>Caterpillar</Name>
        </Stock>
    </StockQuotes>

How would I parse the above mentioned JSON Object to display data in the table as shown in the below?

Symbol:CAT
Last: 80.95
Date: 7-20-2012
Time: 4:01PM
Change:0.00
Ranjith
  • 549
  • 4
  • 10
  • 20

2 Answers2

2

Use an XML parser, not a regex! There are many availble for every language.

If the string had always the same format, you could also use string methods.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

You can use XPath to extract text from specific nodes. The following are the Xpath expressions required to extract the details you want

/StockQuotes/Stock[1]/Symbol[1]
/StockQuotes/Stock[1]/Last[1]
/StockQuotes/Stock[1]/Date[1]
/StockQuotes/Stock[1]/Time[1]
/StockQuotes/Stock[1]/Change[1]

Note: IE5 and later has implemented that [0] should be the first node, but according to the W3C standard it should have been [1]!!

Dungeon Hunter
  • 19,827
  • 13
  • 59
  • 82