Solved

Read Json with JSON.Net

Posted on 2014-10-06
5
239 Views
Last Modified: 2014-10-18
Hello Experts,
I am having one JSON object and I want to read the key and value of that object. Currently I am having following JSon Data and I want to read all the values from that. But Now I am getting complete Object as an string value.
I have used the JSON assemblies from below website
http://james.newtonking.com/json

My JSON OBJECT

"[\r\n  {\r\n    \"MCC\": \"6012\",\r\n    \"posEntryMode\": \"019\",\r\n    \"posCode\": \"05\",\r\n    \"caTid\": \"\",\r\n    \"caId\": \"VJB114100000003\",\r\n    \"caTa\": \"\"\r\n  },\r\n  [\r\n    {\r\n      \"Bio\": \"Y\",\r\n      \"bt\": \"PFBpZCB0cz0iMjAxNC0xMC0wN1QxMDowOToxNyIgdmVyPSIxLjAiPjxCaW9zPjxCaW8gdHlwZT0iRk1SIiBwb3NoPSIiPlJrMVNBQ0F5TUFBQUFBRUNBQUFCQUFHUUFNVUF4UUVBQVFBcEpJRGFBQTlrQUlDZEFCdndBRURSQUNCd0FFQzVBREowQUVCZE+PC9QaWQ+\"\r\n    },\r\n    {\r\n      \"udc\": \"\",\r\n      \"pip\": \"10.10.10.211\",\r\n      \"fdc\": \"NC\",\r\n      \"idc\": \"NA\",\r\n      \"lot\": \"P\",\r\n      \"lov\": \"\"\r\n    },\r\n    {\r\n      \"ci\": \"20150922\",\r\n      \"encodedSk\": \"REYS6q48b8H3+2sZLX3NO01n8s6uVOgWkw==\"\r\n    },\r\n    {\r\n      \"type\": \"P\",\r\n      \"encodedValue\": \"+U2YRj7pbVrnm7KsI203mgyxr+MSdV/xG6o1/XWoeiGsST/unbmlHH7sk1CXfytXygZLmXYyY6Qtx0wwAM8Y5QrElOopb\"\r\n    },\r\n    {\r\n      \"encodedHmac\": \"3??3?\\u0012?\\u0001??w?Bi\\u0003?????X??\\u0007??????\\u0018?\\u001fx??????~\\u0013@\\t? \",\r\n      \"uid\": \"\",\r\n      \"pidTs\": \"\"\r\n    }\r\n  ]\r\n]"

Open in new window


Create JSON Object
            Auth auth = new Auth 
            {
                encodedHmac=EncodedHMAC, //"97CjMc2VIsKgrrcbrN19UQHM866PoZHNij6XxxBQUTtAs611UBCXG4pwTB/pXceU",
                uid = AadharNo,
                pidTs = TimeStamp //"2014-09-17T11:23:47"
                
            };
            Uses uses = new Uses 
            {
                Bio="Y",
                bt = bioMetric
            };
            Meta meta = new Meta
            {
                udc = UniqueDeviceCode,
                pip = "10.10.10.211",
                fdc = "NC",
                idc = "NA",
                lot = "P",
                lov = TransactionLocationPinCode

            };
            skey skey = new skey 
            {
                ci = "20150922", // Expiry date of the certificate issued by UIDAI
                encodedSk = SessionKey //"Lax7e1a6e9KkLWmzIDsjg\u003d\u003d"
            };
            DATA data = new DATA 
            {
                type = "P",
                encodedValue = "+U2YRj7pbVrnm7KsI203mgyWa6w96vURim3S6rEjwjw/fY8K+6Qtx0wwAM8Y5QrElOopb"
            };
TransactionInfo trans= new TransactionInfo 
            {
                MCC="6012",
                posEntryMode="019",
                posCode="05",
                caTid=TerminalID,
                caId = "1234",
                caTa = TerminalAddress
            };
Object[] Auths = { uses, meta, skey, data, auth };            
Object[] finalList = { trans, Auths};

Open in new window


Read JSON OBJECT
JsonTextReader reader = new JsonTextReader(new StringReader(json));
            string[] res=new string[30];
            int i=0;
            while (reader.Read())
            {
                if (reader.Value != null)
                {
                    res[i] = reader.TokenType + "=  " + reader.Value;                    
                }                    
                i++;
            }

Open in new window

While reading I am getting TokenType is String and Value contains complete Json string.
0
Comment
Question by:Manoj Patil
  • 3
  • 2
5 Comments
 
LVL 11

Expert Comment

by:louisfr
ID: 40365355
I get an IndexOutOfRangeException, because the array is half the required size.
Are you sure your json variable contains the string you mention?
Which version of Json.NET are you using?
0
 
LVL 19

Accepted Solution

by:
Manoj Patil earned 0 total points
ID: 40365383
I am using Json.NET 6.0.0.0
As you are getting array size issue. It is ok.. We can change the size.  But while reading the values of JSON I can't differentiate the name and it's value...
Complete JSON object I am getting as an string.

For e.g :
I actually want the below values
encodedHmac="97CjMc2VIsKgrrcbrN19UQHM866PoZHNij6XxxBQUTtAs611UBCXG4pwTB/pXceU",
uid = "6546546654546",
pidTs = "2014-09-17T11:23:47"

But for now I am getting whole JSON as an string.. as  mentioned in the question
0
 
LVL 11

Expert Comment

by:louisfr
ID: 40366142
Something like the following?
JsonTextReader reader = new JsonTextReader(new StringReader(json));
string[] res = new string[30];
int i = 0;
string property = null;
while (reader.Read())
{
    if (reader.Value != null)
    {
        if (reader.TokenType == JsonToken.PropertyName)
            property = (string)reader.Value;
        else
        {
            res[i] = property + "=  " + reader.Value;
            i++;
        }
    }
}

Open in new window

0
 
LVL 19

Author Comment

by:Manoj Patil
ID: 40367778
OK... Let's try this..
I will get back to you shortly...
0
 
LVL 19

Author Closing Comment

by:Manoj Patil
ID: 40388452
Found my own solution
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
The task of choosing a web design company to build a website for your business should never be taken in a light manner. Provided the fact that your website will act as a representative to your business and will be responsible for imposing an online …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now