1
temp= {
       "Vikas": 1,
       "Pravin": 2,
       "Akshay": 5,
       "Vijay": 3,
       "Prasad": 4 
      }

This is value I am getting in temp variable I want to store this data in database with one additional column like following

name      rank   createdby
vikas      1       nitin
pravin     2       nitin
akshay     5       nitin
vijay      3       nitin
Prasad     4       nitin


Note:  name column is employee name so it is anything 

I tried like getting in C#

[System.Web.Services.WebMethod]
public static string setAnswer(string jsondata)
{
    List<string[]> data = JsonConvert.DeserializeObject<List<string[]>>(jsondata);
}

Question.

1) how to get this json data in C# and add additional column

2) Is it possible to pass whole data using multiple row insert in one query instead of using loop and insert single row.

9
  • 4
    What type db access are you using? EF ? ADO? NHibernate? Commented Feb 6, 2017 at 10:58
  • 1
    depends on the database you are using. If the db allows bulkinsert its definitly possible. to add a row to an array you have to copy the whole array. Commented Feb 6, 2017 at 11:00
  • 3
    upgrade to sql server 2016 Commented Feb 6, 2017 at 11:00
  • 1
    yes to avoid for loop you can use table valued parameters you need to convert list to datatable check this link blog.sqlauthority.com/2008/08/31/… let me know if you require more help Commented Feb 6, 2017 at 11:02
  • 2
    I assume temp = is your way of saying that you have a variable with that content, not that this is part of the payload/string because this part is not legal json. Commented Feb 6, 2017 at 11:10

3 Answers 3

3

The correct way to deserialize this content:

{
    "Vikas": 1,
    "Pravin": 2,
    "Akshay": 5,
    "Vijay": 3,
    "Prasad": 4 
}

Is to deserialize it into a Dictionary<string, int>.

Here's a LINQPad example:

void Main()
{
    const string json = @"{
        ""Vikas"": 1,
        ""Pravin"": 2,
        ""Akshay"": 5,
        ""Vijay"": 3,
        ""Prasad"": 4
    }";
    Dictionary<string, int> empsAndNumbers =
        JsonConvert.DeserializeObject<Dictionary<string, int>>(json);
    empsAndNumbers.Dump();
}

which outputs:

example output

Sign up to request clarification or add additional context in comments.

Comments

3

Edited after explantions:

You can deserialize it to dictionary in this way:

var data = JsonConvert.DeserializeObject<Dictionary<string, int>>(temp);

I this point we have dictionary with data. How to save it into DB depends how you access to db.

5 Comments

thanks for reply but no..no... this is employee name so i cannot make it static it may be anything so this is not possible to make class
A dictionary of string to int is more appropriate, Dictionary<string, int>.
@Pravin you don say it, this is very imprtant thing for solution. Edit question and add this info.
@LasseV.Karlsen with this info you are right
okk I will add in question
2

Create an Entity : MemberRank

public class MemberRank
{
    public int Id { get; set; }
    public string MemberName { get; set; }
    public int Rank{ get; set; }
    public string CreatedBy { get; set; }
}

Then in Controller,

var membersText = File.ReadAllText(Path.Combine(envContentRootPath, @"PathOfTheFile"));
var members = JsonConvert.DeserializeObject<List<MemberRank>>(membersText);
members.ForEach(cb => cb.CreatedBy = "nitin");
_dbContext.Set<MemberRank>().AddRange(members);
_dbContext.SaveChanges();

For this to work, you need to change your json file as follows:

[
  {
     "memberName" : "Vikas",
     "rank" : 1
  },
  {
     "memberName" : "Pravin",
     "rank" : 2
  },
  {
     "memberName" : "Akshay",
     "rank" : 5
  },
  {
     "memberName" : "Vijay",
     "rank" : 3
  },
  {
     "memberName" : "Prasad",
     "rank" : 4
  }
]

3 Comments

How do you get Json.net to deserialize the example json into your MemberRank type?
Where do you have in question info about EF?
I am assuming that he is using EF, Sir.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.