Solved

Read Json with JSON.Net

Posted on 2014-10-06
5
260 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

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

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

719 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