Solved

Read Json with JSON.Net

Posted on 2014-10-06
5
234 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 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

9 Experts available now in Live!

Get 1:1 Help Now