0

I want to convert this web service string to JSONArray , but it seems to have a problem , although I've validated it.

[
 {
"hireDate": null,
"homePhoneNumber": null,
"gender": null,
"city": null,
"mobileNumber": null,
"idNumber": 123,
"religion": null,
"leftOver": 1,
"annualVacations": 5,
"dob": null,
"name": null,
"rank": 0,
"id": 1,
"workingHours": [],
"email": "[email protected]",
"managers": [],
"alternativeMobileNumber": null,
"activated": true,
"username": "[email protected]"
},
{
"hireDate": null,
"homePhoneNumber": null,
"gender": null,
"city": null,
"mobileNumber": null,
"idNumber": 123,
"religion": null,
"leftOver": 1,
"annualVacations": 5,
"dob": null,
"name": null,
"rank": 0,
"id": 11,
"workingHours": [],
"email": "[email protected]",
"managers": [],
"alternativeMobileNumber": null,
"activated": true,
"username": "[email protected]"
}
]

My Code:

JSONArray js = new JSONArray(payload);

what should the format of payload be to create the JSONArray ?

6
  • please post more code and what errors/problems you have, because the code looks correct and JSON also looks correct. explain more what is the problem Commented Dec 27, 2016 at 13:08
  • What is payload value? Commented Dec 27, 2016 at 13:09
  • JSONArray accept on constructor: docs.oracle.com/javaee/7/api/javax/json/JsonArray.html Commented Dec 27, 2016 at 13:12
  • @Milaci payload is the JSON string , and my question is what the format of the string in it ? Commented Dec 27, 2016 at 13:15
  • Please tag the question with the language you are using to parse JSON. I suppose it is Java. Commented Dec 27, 2016 at 13:15

2 Answers 2

1

Looking at the code it looks like it should work. Following sample worked for me (The JSON is taken as a string for testing).

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Main {

public static void main(String[] args) throws JSONException {
    String test = "[{\"hireDate\": null,\"homePhoneNumber\": null,\"gender\": null,\"city\": null,\"mobileNumber\": null,\"idNumber\": 123,\"religion\": null,\"leftOver\": 1,\"annualVacations\": 5,\"dob\": null,\"name\": null,\"rank\": 0,\"id\": 1,\"workingHours\": [],\"email\": \"[email protected]\",\"managers\": [],\"alternativeMobileNumber\": null,\"activated\": true,\"username\": \"[email protected]\"},{\"hireDate\": null,\"homePhoneNumber\": null,\"gender\": null,\"city\": null,\"mobileNumber\": null,\"idNumber\": 123,\"religion\": null,\"leftOver\": 1,\"annualVacations\": 5,\"dob\": null,\"name\": null,\"rank\": 0,\"id\": 11,\"workingHours\": [],\"email\": \"[email protected]\",\"managers\": [],\"alternativeMobileNumber\": null,\"activated\": true,\"username\": \"[email protected]\"}]";

    JSONArray jsonArray = new JSONArray(test);

    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject explrObject = jsonArray.getJSONObject(i);
        System.out.println(explrObject.getString("username"));

    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I would recommend you to follow this link to get your problem solve.

http://www.java67.com/2016/10/3-ways-to-convert-string-to-json-object-in-java.html

Comments

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.