0

I am new to JSON and getting confused everytime I create a new one. I am trying to create a JSON array like this :

{
      "id":"2003",
      "HouseMD" : 
            {
              "Doctor_1": "Thirteen",
              "Doctor_2" : "Chase"
              "Doctor_n" : "Someone"
            }

}

Basically I am trying to add info dynamically from Doctor_1 to Doctor_n" in a for loop. and if I use a JSON Object I am only getting the last value when I finally print it. How do I get something that I want? Any help appreciated.

Thanks.

M G
  • 1,240
  • 14
  • 26
JPro
  • 6,292
  • 13
  • 57
  • 83
  • 1
    I think you don't have to store HouseMD's in object, you should use array instead. You know that there should be doctors, so there is no point in keeping keys like 'Doctor_1', _2 etc. Just do `{ id: 1, HouseMD: [ 'Thirteen', 'Chase', 'Someone' ] }` Or, if you want to store more properties for doctors, then it could be array of doctors objects `[ {name: 'Chase', otherProp:'val'}, ...]` – Wirus Aug 17 '13 at 20:36
  • this post may help: http://stackoverflow.com/questions/15402321/how-to-convert-hashmap-to-json-array-in-android – Abdullah Shoaib Aug 17 '13 at 20:37

2 Answers2

0

JSON arrays look like this:

{ "id":"2003", "HouseMD" : [{ "Doctor_1": "Thirteen"}, {"Doctor_2" : "Chase"}, {"Doctor_n" : "Someone" }]}

Notice the square bracket that surrounds each JSON object in the array.

Here is the link to the JSON website, which can offer more info:

JSON

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
  • How do I construct a JSON like this? and how can I retrive this on the server side? – JPro Aug 18 '13 at 21:42
0

Note that in order for the code below to work, you will also need the JSON library, which you can easily download from here Download Java JSON Library

I don't know the approach you are using, but based on the format you want, I would do something like this:

JSONObject data = new JSONObject();

data.put("id", "2003");

JSONObject doctors = new JSONObject();

//here I suppose you have all doctors in a list named doctorList
//and also suppose that you get the name of a doctor by the getName() method
for (int i = 0; i < doctorList.size(); ++i)
{
   doctors.put("Doctor_" + (i+1), doctorList.get(i).getName();
}

data.put("HouseMD", doctors);

//then you could write to a file, or on screen just for test
System.out.println(data.toString());

However, I feel you need to become more comfortable with JSON, so try starting here.

BitParser
  • 3,748
  • 26
  • 42
  • I tried sending this to server and server returns back saying Malformed UTF-8 characters, possibly wrong encoding, what to do – JPro Aug 18 '13 at 21:42
  • @JPro IDK why I thought you were using Java. What language are you using on client side & on server side and I might help you. Perhaps the code you've tried so far.. – BitParser Aug 19 '13 at 03:38
  • On the mobile end, I am using java, on the server side, I am using PHP and this is where I am tyring to validate the received JSON string, and the php code prints the error message I mentioned above – JPro Aug 20 '13 at 10:50
  • That means you have a problem with the encoding. Could you please post some code? – BitParser Aug 22 '13 at 07:26