Solved

Read Json with JSON.Net

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

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 article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

730 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