mantic sol
mantic sol

Reputation: 53

Replace json brackets using c sharp?how to

here is my json string

  {[ 
  {
    "CurrencyName": "nomi"  
    }, 
   { 
   "CurrencySign": "%%" 
     },
   {
    "PositiveFormat": "2"
   },
    {
       "NegativeFormat": "3"
    },
   {
    "CurrencyStatus": "45"
   }
  ]}

i want to replace starting and ending bracket 2 these are

 {[ 
 ]}

only and then i want to store json in variable my c sharp code is here

dynamic objEnteryVal = objEntry.GetValue("models")[0]["models"];

only i have to replace first and last two brackets using .replace

Upvotes: 1

Views: 186

Answers (1)

Saurabh Chauhan
Saurabh Chauhan

Reputation: 144

Hey I dont think replace will help a lot I means you have to customise replace function because there is only replace not replaceAT function available in C#. What you can do is use substring function as below.

        string JSON = "{[SAME]}";
        int startPos=JSON.IndexOf("[") + 1;
        int LastPos=JSON.LastIndexOf("]");
        int length = JSON.Length - startPos - (JSON.Length - LastPos);
        JSON = JSON.Substring(startPos, length);

This will get you the result "SAME" out of {[SAME]}.

Upvotes: 1

Related Questions