JavaScript I know-flattening Json objects

Source: Internet
Author: User
Introduction: In Asp. under the netmvc framework, submitting a complex Json object to the Action method using the Ajax method may cause some problems. Here we use Javascript to solve this problem. In asp. after netmvc3.0, you can use JsonValueProviderFactory to solve the problem... syntaxHigh

 

Introduction:

Some problems may occur when submitting a complex Json object to the Action method using the Ajax method in the Asp.net mvc framework. Here we use Javascript to solve this problem, after asp.net mvc 3.0, you can use JsonValueProviderFactory to solve this problem, but here we provide a solution to solve this problem with pure js. This method can also be used in the old mvc 2.0 system.

You need to submit a complex object, such as the following object structure:

1 {

2 parameter name: 'abc ',

3. Destination Address: '72, Haidian Road, Beijing ',

4 Rooms: [{roomName: 'standard interval', roomPrice: 720 },

5 {roomName: deluxe room, roomPrice: 1020}],

6 stars Star: 4

7}

8 // at this time, we must convert it to the following format to correctly submit it to the background Action object.

9

10

11 {

12 Parameter Name: 'abc ',

13. Destination Address: '72, Haidian Road, Beijing ',

14 Rooms [0]: {roomName: 'standard interval', roomPrice: 720 },

15 Rooms [1]: {roomName: deluxe room, roomPrice: 1020 },

16 stars: 4

17}

18 // format conversion code:

19

20 var Convert = {

21 _ jsonFlat: function (data, parentPro, returnObj ){

22 if (data instanceof Object ){

23 for (varpro in data ){

24 try {

25 varproValue = eval ("data." + pro. toString ());

26 if (proValue instanceof Array ){

27 for (var I = 0; I

28 if (parentPro ){

29 Convert. _ jsonFlat (proValue [I], parentPro + "." + pro + "[" + I + "]", returnObj );

30}

31 else

32 Convert. _ jsonFlat (proValue [I], pro + "[" + I + "]", returnObj );

33}

34. continue;

35}

36 if (proValue instanceof Object ){

37 if (parentPro)

38 Convert. _ jsonFlat (proValue, parentPro + "." + pro, returnObj );

39 else

40 Convert. _ jsonFlat (proValue, pro, returnObj );

41

42. continue;

43}

44 if (parentPro)

45 returnObj [parentPro + "." + pro] = proValue;

46 else

47 returnObj [pro] = proValue;

48

49}

50 catch (e ){};

51}

52 return;

53}

54 // otherwiselike string/int/datetime format

55 returnObj [parentPro] = data;

56}, jsonFlat: function (data ){

57 // debugger;

58 if (data & data instanceof Object ){

59 varretObj = {};

60 Convert. _ jsonFlat (data, null, retObj );

61 returnretObj;

62}

63 return null;

64 },

65}

Explanation: the above Code converts the Json object format. Only the converted complex Json object can be submitted to the background Action method. The JsonFloat method uses recursion to traverse all attributes on the json object for flat conversion.

Call example

View sourceprint?

1 $. ajax ({

2 url: "controller/action ",

3 data: Convert. jsonFlat ({/* your json data */}),

4 success: function (){

5 $ (this). addClass ("done ");

6}

7 });

After JsonValueProviderFactory has been registered in Mvc 3.0, and then the ajax method can be called directly without flattening using js:

Note that contentType must be set to application/json.

View sourceprint?

$. Ajax ({

Url: "controller/action ",

Data: {/* your json data */},

ContentType: "application/json ",

Success: function (){

$ (This). addClass ("done ");

}

});

 

Postscript:

[Share today's mood]: After dinner at noon today, I went back to cnblogs and saw a girl's post on an algorithm. Amazed that a girl can be so persistent in technology,

At this time, I was seen by the boss and told me Robbin. You should not hold on to the idea of algorithm supremacy every day to see if I don't understand algorithms and do not earn more than you.

At this time, I did not dare to speak back, but there was a picture in my mind:

A contractor shouted at a migrant worker: "Don't think that you are earning much if you are strong. If you are not strong enough, you don't earn more than you ."

November rain

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.