Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

jquery - How do I restructure JSON's name value pair

I have a page that uses indexes on input names. When serializing the form the blow input comes as an output (stringified as JSON).

[
    {
        "name": "ProductsWithPackage[0].PackageName[0]",
        "value": "Package Name"
    },
    {
        "name": "ProductsWithPackage[1].PackageName[1]",
        "value": "Package Name 2"
    },
    {
        "name": "ProductsWithPackage[2].PackageName[2]",
        "value": "Package Name 3"
    },
    {
        "name": "ProductsWithPackage[0].PackageName[0].ProductID[0]",
        "value": "11FFGBAG01"
    },
    {
        "name": "ProductsWithPackage[0].PackageName[0].ProductID[1]",
        "value": "11FFLZPN01"
    },
    {
        "name": "ProductsWithPackage[0].PackageName[0].ProductID[2]",
        "value": "11FFPLER01"
    },
    {
        "name": "ProductsWithPackage[1].PackageName[1].ProductID[0]",
        "value": "11FFGBAG01"
    },
    {
        "name": "ProductsWithPackage[1].PackageName[1].ProductID[1]",
        "value": "11FFLZPN01"
    },
    {
        "name": "ProductsWithPackage[2].PackageName[2].ProductID[0]",
        "value": "11FFGBAG01"
    }
]

You'll not that it follows the hierarchy - ProductsWithPackage > PackageName > ProductID (may not be there)

I wanted to send the model back to server where the class structure kind of recognize the structure and get the values. But unfortunately since the name is all string so it can't parse the structure correctly.

I think I would have to redesign the JSON to something more suitable according to the model I'm using at the backend. So below would work in this case, But I don't know how to I parse or manipulate the above JSON as below desired output.

[
    {
        "ProductsWithPackage": [
            {
                "PackageName": [

                    {
                        "Name": "PakckageName",
                        "ProductID": [
                            "11FFGBAG01",
                            "11FFPLER01"
                        ]
                    }
                ]
            }
        ]
    }
]
question from:https://stackoverflow.com/questions/65920673/how-do-i-restructure-jsons-name-value-pair

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...