Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Read Json with JSON.Net

Posted on 2014-10-06
5
Medium Priority
?
294 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 12

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 12

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

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

571 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