0

I am working on MVC 5 project.

string hdPrimUserFirstReg = "{\"ProjectName\":\"wwwwwww\",\"ClientName\":\"asdw\",\"ProjectType\":\"2\",\"ProjectLocation\":\"asdfasdfs\",\"Status\":\"Completed\"},\"Form6\":null}","hdPremUserProject":"","hdclientLogo":"","hdProjectTitle":"","ProjectName":"eeeeeee","ClientName":"asdwedfswe","ProjectType":"1","ProjectLocation":"dfwea","Status":"Completed}";

enter image description here

I want to create a list of ProjectName from the variable hdPrimUserFirstReg JSON format data and also from the Form 5 into the public IList<string> listProjectName { get; set; }

And this is my java-script,

AddProjectList: function (e) {

        debugger;
        if (this.validateForm()) {
            debugger;
            var hidvalue = $('#hdPrimUserFirstReg').val();//// hidden field from same form 
            var hidJson = JSON.parse(hidvalue);
            var json = {};

            $.each($('#frmSubmitPremUserRegFirst').serializeArray(), function (i, field) {
                json[field.name] = field.value || '';
            });           

            hidJson.Form5 = json;

            var str = JSON.stringify(hidJson);

            $('#hdPrimUserFirstReg').val(str); // hidden field from same form 

        }
    },

Error: cannot deserialize the current json object (e.g. {“name”:“value”}) into type 'system.collections.generic.list`1

How can I do this ?

Please help me...

3
  • your json string is invalid Commented Mar 22, 2017 at 7:13
  • the string contain long value. So... I want to get only 'Projectname' from the json string. Commented Mar 22, 2017 at 7:15
  • I have updated my question. Commented Mar 22, 2017 at 9:34

2 Answers 2

1
var hdPrimUserFirstRegList = 
          JsonConvert
             .DeserializeObject<List<hdPrimUserFirstRegModel>>(pageVM.hdPrimUserFirstReg );
var projectsNames = hdPrimUserFirstRegList.Select(x=>x.ProjectName).toList();

And create model hdPrimUserFirstRegModelcontains all json object properties.

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

3 Comments

no build error, but occurred run time error: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1'
@AbhiJA because this is not a valid json, you can test it online first jsonlint.com
sir, could you please check my jscript code. I have updated my question. There is hiddenfield 'hdPrimUserFirstReg' that holding data by form 1,Form 2 etc. So I can not rearrange json inside same hiddenfield.
1

Try this

var listProjectName = JsonConvert .DeserializeObject<List<hdPrimUserFirstRegModel>>(pageVM.hdP‌​rimUserFirstReg ).Select(x => x.ProjectName).ToList()

5 Comments

The 'hdPrimUserFirstReg ' inside 'Form5'. How can I get ?
pageVM.hdPrimUserFirstReg
okay, but I want creat only list of projectname, but the pageVM.hdPrimUserFirstReg contain lot of items.
ok, var listProjectName = JsonConvert .DeserializeObject<List<hdPrimUserFirstRegModel>>(pageVM.hdPrimUserFirstReg ).Select(x => x.ProjectName).ToList() ;
no build error, but occurred run time error: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1'

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.