1

I' very new to json, I can't seem to access it right variables.

I'm trying to get "username" for example, so that I can create mocks for AngularJS project.

$httpBackend.expectGET('user.json').
  respond(Users[ { Username: "bjarnipolo"}, { Username: "yolo"} ]);


{
  "Users":[
      {
      "UserId":1,
      "Username":"bjarnipolo",
      "Password":null,
      "FirstName":"Bjarni Póló",
      "LastName":"Súkkulaðisson",
      "Ssn":"2412813539",
      "Email":"bjarnip08@ru.is",
      "Phone":"6903066",
      "Roles":"Stjornandi",
      "JobTitle":"Scrum Master",
      "ImageUrl":"http://us.cdn4.123rf.com/168nwm/nruboc/nruboc0802/nruboc080200034/2557282-a-small-cute-dog-playing-basketball-over-a-black-background.jpg",
      "IsActive":true
      },
      {
      "UserId":2,
      "Username":"yolo",
      "Password":null,
      "FirstName":"Brynjólfur YOLO",
      "LastName":"Hermannsson",
      "Ssn":"0106752040",
      "Email":"Binni@example.com",
      "Phone":"8995555",
      "Roles":"Notandi",
      "JobTitle":"Team member",
      "ImageUrl":"http://us.cdn4.123rf.com/168nwm/nruboc/nruboc0802/nruboc080200034/2557282-a-small-cute-dog-playing-basketball-over-a-black-background.jpg",
      "IsActive":true
      }
  ]
}
ArniReynir
  • 849
  • 3
  • 12
  • 29
  • What does "f.x" mean? How did you try to achieve your goal? Please improve your question to get more helpful answers. – thSoft Jun 10 '13 at 12:27
  • respond( { Users[0].Username :"bjarnipolo"} ); gives me error : Uncaught SyntaxError: Unexpected token [ – ArniReynir Jun 10 '13 at 12:28
  • @ArniReynir try to create a new user, then NewUser = Users[0];, NewUser.UserName = "dsddsd"; AND User[0] = NewUser.. Try this – Vinc 웃 Jun 10 '13 at 12:31
  • { Users[0].Username :"bjarnipolo"} is the syntax to create new object. And "Users[0].Username" is an invalid property name – Khanh TO Jun 10 '13 at 12:32
  • Hi, please see this link http://stackoverflow.com/questions/6964403/parsing-json-with-php. – Gaston Flores Jun 10 '13 at 12:58

1 Answers1

1

I think I found a solution to get what you want.

You can use new Request.JSON. The request should look like this :

new Request.JSON({
 url: '/echo/json/',
 data: {
    json: JSON.encode({
          "Users":[
              {
              "UserId":1,
              "Username":"bjarnipolo",
              "Password":null,
              "FirstName":"Bjarni Póló",
              "LastName":"Súkkulaðisson",
              "Ssn":"2412813539",
              "Email":"bjarnip08@ru.is",
              "Phone":"6903066",
              "Roles":"Stjornandi",
              "JobTitle":"Scrum Master",
              "ImageUrl":"http://us.cdn4.123rf.com/168nwm/nruboc/nruboc0802/nruboc080200034/2557282-a-small-cute-dog-playing-basketball-over-a-black-background.jpg",
              "IsActive":true
              },
              {
              "UserId":2,
              "Username":"yolo",
              "Password":null,
              "FirstName":"Brynjólfur YOLO",
              "LastName":"Hermannsson",
              "Ssn":"0106752040",
              "Email":"Binni@example.com",
              "Phone":"8995555",
              "Roles":"Notandi",
              "JobTitle":"Team member",
              "ImageUrl":"http://us.cdn4.123rf.com/168nwm/nruboc/nruboc0802/nruboc080200034/2557282-a-small-cute-dog-playing-basketball-over-a-black-background.jpg",
              "IsActive":true
              }
         ]
     }),
 },
 onSuccess: function(response) {
    var jsonObj = response;
    alert(jsonObj.Users[1].Username);

    jsonObj.Users[1].Username = "dsds";

    alert(jsonObj.Users[1].Username);
 }
}).send();

Here is the jsFiddle.

Vinc 웃
  • 1,187
  • 4
  • 25
  • 64