0

I am facing a problem with formatting JSON as response data. I have a class Player, which has the fields name, id, age, top, middle, and bottom. I have another class Position which has fields id, top, middle, and bottom. Player fields top, middle, and bottom come from Position and set it for Player.

My problem is I am getting JSON response data as:

{ id     : 10,
  age    : 16,
  top    : 18,
  middle : 16,
  bottom : 10
}

Whereas I want it as:

 { id  : 10,
   age : 16,
   position : {
     top    : 18,
     middle : 16,
     bottom : 10
   }
 }
5
  • its depends upon your array formation Commented Jan 17, 2012 at 6:54
  • Where do you get your response from? What server-side language are you using to produce this output? Is it PHP, Ruby, VB.NET/C#? Commented Jan 17, 2012 at 6:56
  • diEcho: this is not array, actually its fields from other class. Commented Jan 17, 2012 at 7:02
  • NoLifeKing: am using java RestEasy api Commented Jan 17, 2012 at 7:02
  • Then you should be able to set the player-class to contain the position as a property. :) Commented Jan 17, 2012 at 7:03

1 Answer 1

1

You have not told how the JSON object is generated as an output. Here you can crate new JSON object as you wanted by organizing the data from the returned out put like given below:

var returned={
    "id": 10,
    "age": 16,
    "top": 18,
    "middle": 16,
    "bottom": 10
};

var iWanted={
  "id":returned["id"],
  "age":returned["age"],
  "position":{
        "top":returned["top"],
        "middle":returned["middle"],
        "bottom":returned["bottom"]
      }
};

console.log(iWanted);
Sign up to request clarification or add additional context in comments.

3 Comments

Json object is created using restAPI @Produces("application/json").
and am not getting how to convert returned as iWanted using it
Are you able to assign the restAPI response to any variable ? Could you please elaborate how the response done in restAPI ?

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.