0

I have following knockout text binding :

 <td><strong><span id="texthotelcode" data-bind="text: parameters"
 /></strong></td>

data binding of text: which returns data: {"id1":"2Z94","id2":"9861"}

now I want to convert them from this JSON into Key and value in Dictionary in C# as string, string

Any idea for this case thanks

TrangZinita
  • 107
  • 1
  • 9
  • 1
    Have a look at this question: http://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net?lq=1 – andyp May 26 '14 at 17:49
  • @andyp The question you linked to uses json.net. Maybe user3520221 did not want to use this third party assembly... When .net does it itself – GôTô May 27 '14 at 08:51
  • @GôTô One of the answers recommends using a library, the others offer alternatives. – Roman May 27 '14 at 12:02
  • @R0MANARMY All of them need c# code for the deserialization, because most of them are very old answers... .net can now deserialize without having to write more c# code. The question is 5 years old, things have changed – GôTô May 27 '14 at 12:06
  • @GôTô You should provide a reference in your answer that this is now something that's built in. And possibly add an answer to the other question so others can find it easier. – Roman May 27 '14 at 12:33
  • @R0MANARMY Answer updated, I will post an answer on the other thread when I get a chance. Thank you for your recommendations – GôTô May 27 '14 at 12:39
  • @GôTô I meant I tried to find a reference that this is now built into .NET and couldn't. Would you mind including that in your answer? – Roman May 27 '14 at 12:53
  • @R0MANARMY Oh. Actually I don't know when it was added, I just know that's how I do it :) I will look for a reference – GôTô May 27 '14 at 12:55

1 Answers1

0

.net can deserialize JSON in the following form into c# dictionary:

dict: [ 
         { "Key": "id1", "Value": "2Z94" },
         { "Key": "id2", "Value": "9861" }
      ]

So you can use a function like this one to convert your object:

function toDictionary(data) {
    var dict = [];
    for (var prop in data)
        dict.push({ "Key": prop, "Value": data[prop] });
    return dict;
}

Then just send this object to the server.


Note that as andyp pointed out, there is a similar question with answers in this thread.

When in search for an answer, please search the site first, and post your question only when no answer fits your needs. In this case, the other thread might need some update.

Community
  • 1
  • 1
GôTô
  • 7,974
  • 3
  • 32
  • 43
  • Hi @GôTô : Thanks for your answer, I have fixed my page following this url : http://jsfiddle.net/rniemeyer/7yDTJ/ however, what I want to have is key and value are listed on the view page, as Im quite new to knockoutJS, could you please suggest me some ideas in this case ? Thanks – TrangZinita May 27 '14 at 12:59
  • @user3520221 You want to display the dictionary on the page? Or... I don't get it :) – GôTô May 27 '14 at 13:10
  • Yes I wish to display the Dictionay on the page – TrangZinita May 27 '14 at 13:11
  • Thanks a lot for your example, I have changed the page to display the key and value separately, I dont know if we have any prop in order to do so in toJSON function ? http://jsfiddle.net/z7d85/ – TrangZinita May 27 '14 at 13:20
  • @user3520221 join me there to chat: http://jsfiddle.net/nyothecat/v9BNr/#&togetherjs=5olOJyaK4N – GôTô May 27 '14 at 13:26
  • Thanks GoTo a lot for your help, but sorry I have made it unclear that I wish to have the key and value for each line, like [reference] [2Z94] and one line for [car_id] [9861] – TrangZinita May 28 '14 at 07:56
  • Thank you, GoTo. I could understand and implement what I got sofar, thanks for your help. In fact, I have one more wonder and I hope you can suggest me the idea, the dictionary that I have now is not set before anymore, it comes from data-bind="text: parameters" and parameters contains : { "Key": "id1", "Value": "2Z94" } so how can I have one function like transferToDict(parameters) and put in the data-bind then have a loop to get all keys values, line by line on the HTML Page ? Thanks – TrangZinita May 28 '14 at 14:21