JSON object manipulation

前端 未结 1 1290
陌清茗
陌清茗 2021-01-12 20:26

Let\'s suppose that we have the following JSON Object that describes a Person:

{
 \"firstName\": \"John\",
 \"lastName\": \"Smith\",
 \"age\": 25,
 \"address         


        
相关标签:
1条回答
  • 2021-01-12 20:39

    just parse it and change it

    hash = JSON.parse(json_data)
    hash["firstname"] = "John"
    hash.delete("lastname")
    new_json = hash.to_json
    

    P.S. JSON.parse might not work - it depends on which JSON library you're using. try this instead:

    ActiveSupport::JSON.decode(json_data)
    
    0 讨论(0)
提交回复
热议问题