-2

How to take value from this kind of long JSON file in c# currently I am using dynamic variable in c# but it will not work if I need latest JSON transaction because dynamic variable hold value of a particular location but its local will increase after processing a new transaction and I need destination tag so what should I do for that .

{  
  "result":"success",
  "count":5,
  "transactions":[  
    {  
      "hash":"866B841C8C3B2D840497679FAC45A7D4341E12056AAA53F67471CA1B955FB58E",
      "ledger_index":37860930,
      "date":"2018-04-10T12:35:32+00:00",
      "tx":{  
        "TransactionType":"Payment",
        "Flags":2147483648,
        "SourceTag":0,
        "Sequence":20,
        "DestinationTag":0,
        "LastLedgerSequence":37860958,
        "Amount":"25000000",
        "Fee":"12",
        "SigningPubKey":"02917EB4353C539A2B0766EFAB1723E5BB744516C673FFCF21457DAD44BCECFE25",
        "TxnSignature":"304402202FA9FF18A572ACA1735B54CF5A69E10398EAF7003F3573E402ABA09B404F765C0220773481B494A941CC0F5338836A6B71F5FA2499943C41D50205C8EF158651BD86",
        "Account":"rLcqTPYFSJBWGmu2ZXh8JTYGgdXRPjWTrx",
        "Destination":"rfginNsDts73fZtzp858dT6Y6U67Y1QB7GV"
      },
      "meta":{  
        "TransactionIndex":1,
        "AffectedNodes":[  
          {  
            "CreatedNode":{  
              "LedgerEntryType":"AccountRoot",
              "LedgerIndex":"1C75CD43D75614E00025102355ADCCCF6302FAD5B82DC85F5AAF5696E4E39EC4",
              "NewFields":{  
                "Sequence":1,
                "Balance":"25000000",
                "Account":"rfginNsDt73fZtzp858dT6Y6U6s7Y1QB7GV"
              }
            }
          },
          {  
            "ModifiedNode":{  
              "LedgerEntryType":"AccountRoot",
              "PreviousTxnLgrSeq":37757840,
              "PreviousTxnID":"C794D81D17DF1EA735200E50B9CEADDF19160841E1321F08FDB7C7C4D6BC41AF",
              "LedgerIndex":"BE486CB1FED1B1805E4C42E8E96C73CFDA4E520C66BACEBBE441C1F8212D980E",
              "PreviousFields":{  
                "Sequence":20,
                "Balance":"537421240"
              },
              "FinalFields":{  
                "Flags":0,
                "Sequence":21,
                "OwnerCount":0,
                "Balance":"512421228",
                "Account":"rLcqTPYFSJBWGmu2ZXh8JTYGgdXRPjWTrx"
              }
            }
          }
        ],
        "TransactionResult":"tesSUCCESS",
        "delivered_amount":"25000000"
      }
    },
    {  
      "hash":"86E5D976DDCBF97047C86996286751B23C4384A8B4F6A04E38A57F30F54749CC",
      "ledger_index":38906860,
      "date":"2018-05-25T10:04:22+00:00",
      "tx":{  
        "TransactionType":"Payment",
        "Flags":2147483648,
        "Sequence":28534,
        "DestinationTag":2150,
        "Amount":"25000000",
        "Fee":"45",
        "SigningPubKey":"",
        "Account":"rG2uw1W9FXoiY4QiNqej5WeRxnoBEw4JbL",
        "Destination":"rfginNsDt73fZtzp858dT6Y6U67Y1QB7GV",
        "Signers":[  
          {  
            "Signer":{  
              "SigningPubKey":"032DCABB7A17A96E826435B461E474CB33C86C98B2281D304367FCFC6DE463A215",
              "TxnSignature":"3045022100F9802AE2F7800D42233A5B63999C0BD7AE5B7D60D8244C949FF81EA39959B4BC02205E37DD331ACE2637F2BEE758B5AD642D259F4140E8DE53EF610B6F58745BCC05",
              "Account":"rDc4zYyxMbXfUXNKw6zfM88RBUd9y8uPYX"
            }
          },
          {  
            "Signer":{  
              "SigningPubKey":"03019752C3ABCB2BC89EC5FAEF55D72AE940820A30FBA079D2BB8CE8841174EF21",
              "TxnSignature":"30450221009C7F9519912D494C9F009130927E1248BE6C2F877B1A913B49B8BFD926442F6802205377D1D242D07033DC6FDA8C93BC65638D636A87539F32A0D269AA9B537FCB0D",
              "Account":"rKB5N7FxvcwHSYq4NCKjyMpjML1MznMrFc"
            }
          }
        ]
      },
      "meta":{  
        "TransactionIndex":17,
        "AffectedNodes":[  
          {  
            "ModifiedNode":
Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
Jitender
  • 330
  • 2
  • 11
  • 1
    you could try deserializing to a class object...that will make it easier for you to do operation.... – Hari Govind May 29 '18 at 06:33
  • i am using api this json is the product of api – Jitender May 29 '18 at 06:34
  • that is fine......you want to store this in memory and perform operations on it correct?If so then....you can directly deserialize this json as a class object..... – Hari Govind May 29 '18 at 06:35
  • can you plz send me a link?how to use this json in deserialization – Jitender May 29 '18 at 06:37
  • (deserialization.transactions[3].tx.DestinationTag); i am using this method it is deserializing this json string but this is giving a perticular data at location transaction[3] but every time location will increase so what i should do for that? – Jitender May 29 '18 at 07:01
  • Ya you can make it as a array.....and just feed in the bare bone json.... – Hari Govind May 29 '18 at 07:05
  • update your question with full json structure...maybe one or two items – Hari Govind May 29 '18 at 07:06
  • above is full json...unable to get destination tag of current transaction i am just getting first destination tag which i 0 because i am using deserialization.transactions[3].tx.DestinationTag – Jitender May 29 '18 at 08:10
  • the json is not complete...looks like stackoverflow is trimming it – Hari Govind May 29 '18 at 08:52
  • Possible duplicate of [How can I parse JSON with C#?](https://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c) – Liam Feb 06 '19 at 09:09

1 Answers1

0

There is a nugget package available called Json.Net / Newtonsoft, this package has methods to easily serialize and deserialize json as class objects.

Step 1: Install this module > Install-Package Newtonsoft.Json -Version 11.0.2

Step 2: Next step is to create class that mimics your exact same json with all the key to have the same names, you could use a feature called Paste Special which is available inside Visual Studio.

Create an empty class and copy the JSON to clipboard then click on Edit > Paste Special > Json as Class, this will create a class for you which has the same structure as your JSON.

Step 3: Use the below code to deserialize json to a class object,

JSONClass jsonObjectWithValue = JsonConvert.Deserialize<JSONClass>(jsonString)
Liam
  • 27,717
  • 28
  • 128
  • 190
Hari Govind
  • 369
  • 2
  • 14