2

I have JSON data like this one:

{
   "id":"",
   "firstName":"John",
   "lastName":"Doe",
   "phones":[
      {
         "value":"555-555-555",
         "$$hashKey":"object:8"
      },
      {
         "value":"444-444-444",
         "$$hashKey":"object:10"
      }
   ],
   "emails":[
      {
         "value":"[email protected]",
         "$$hashKey":"object:12"
      },
      {
         "value":"[email protected]",
         "$$hashKey":"object:18"
      }
   ],
   "tags":[
      {
         "value":"friend",
         "$$hashKey":"object:14"
      },
      {
         "value":"john",
         "$$hashKey":"object:16"
      }
   ],
   "address":"Route 44",
   "city":"NY",
   "bookmarked":"",
   "notes":"It's John"
}

I want to store this JSON data to database using ASP.NET MVC entity framework (Using Microsoft SQL Database). My model is auto-generated from database using ADO.NET entity framework data model. Problem that I faced is that for some reason it won't store emails/phones and tags in database. I tried everything that I could find and none of them worked for me. Does someone knows where's the problem and how can I store this JSON to my database?

EDIT 1:

After I debug contact variable in controller I notice that Email, Tags and Phones Model data are empty. I don't know how and why they are empty...

Also I add CONSTRAINT to all foreign keys

Here is database model:

Database model

this is auto-generated model from database (EF data model)

 public partial class Contact
    {
        public Contact()
        {
            this.Emails1 = new HashSet<Email>();
            this.Phones1 = new HashSet<Phone>();
            this.Tags1 = new HashSet<Tag>();
        }

        public int id { get; set; }
        public string firstname { get; set; }
        public string lastname { get; set; }
        public string address { get; set; }
        public string city { get; set; }
        public Nullable<byte> bookmarked { get; set; }
        public string notes { get; set; }

        public virtual ICollection<Email> Emails1 { get; set; }
        public virtual ICollection<Phone> Phones1 { get; set; }
        public virtual ICollection<Tag> Tags1 { get; set; }
    }
}

and here is model For Phone/Tags/Email tables (it's same with different name in one column)

public partial class Email
    {
        public int id { get; set; }
        public int id_contact { get; set; }
        public string email1 { get; set; }

        public virtual Contact Contact1 { get; set; }
    }

and here is function that add data to database:

 public string AddContact(Contact contact)
        {
            if (contact != null)
            {

                db.Contacts.Add(contact);
                db.SaveChanges();
                    return "Contact Added";
            }
            else
            {
                return "Invalid Record";
            }
        }
7
  • What is the error you encounter? Commented Dec 28, 2014 at 10:17
  • @Ofiris there is no error... phones, tags and emails data from JSON object are not stored to database... Commented Dec 28, 2014 at 10:18
  • What if you change those HashSet<T> collections into List<T>? Commented Dec 28, 2014 at 10:21
  • @dbc let me try. I will post back results Commented Dec 28, 2014 at 10:22
  • @dbc I tried what you mentioned and problem it didn't solved my problem Commented Dec 28, 2014 at 10:29

1 Answer 1

4

Your JSON should be look like this:

 var json = {
            "id": "123",
            "firstName": "John",
            "lastName": "Doe",
            "phones": [
               {
                   "number": "555-555-555"
               },
               {
                   "number": "444-444-444"
               }
            ],
            "emails": [
               {
                   "email1": "[email protected]"
               },
               {
                   "email1": "[email protected]"
               }
            ],
            "tags": [
               {
                   "tag1": "friend"
               },
               {
                   "tag1": "john"
               }
            ],
            "address": "Route 44",
            "city": "NY",
            "bookmarked": "",
            "notes": "It's John"
        };

And I have pass it in this way:

$.ajax({
            url: 'YourAddress',
            data: { Action: 'CheckJson', JSONData: JSON.stringify(json) },
            dataType: "json",
            async: false,
            success: function (data) { 
            },
            error: function (x, e) {

            }
        });

Then Download JSON.Net

And do something like this :

string json = request["JSONData"];
Contact m = JsonConvert.DeserializeObject<Contact>(json);

The 'id' should not be empty because by DeserializeObject you will get an exception.

I hope it will help you.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.