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":"[email protected]",
      "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":"[email protected]",
      "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
      }
  ]
}
5
  • 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, 2013 at 12:27
  • respond( { Users[0].Username :"bjarnipolo"} ); gives me error : Uncaught SyntaxError: Unexpected token [
    – ArniReynir
    Jun 10, 2013 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, 2013 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, 2013 at 12:32
  • Hi, please see this link stackoverflow.com/questions/6964403/parsing-json-with-php. Jun 10, 2013 at 12:58

1 Answer 1

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":"[email protected]",
              "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":"[email protected]",
              "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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.