2

I am using the following code read content from server

URL url = new URL("http://dev.dublabs.com:8080/mobileCampus/json/emergencyContacts");
    HttpURLConnection httpcon = (HttpURLConnection)url.openConnection();

    BufferedReader in = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));
    String inputLine;
    while((inputLine=in.readLine())!=null){
        System.out.println(inputLine);
    }

Following is the response from server:

BEGIN:VCARD
VERSION:2.1
FN:Campus Police
N:Campus Police
TEL:555-EDU-HELP
ADR:8230 Boone Blvd.;Bldg 001;;Vienna;VA;22181;
X-MS-OL-DEFAULT-POSTAL-ADDRESS:01155
EMAIL:police@wisconsin.edu
REV:20120501T180000Z
END:VCARD
BEGIN:VCARD
VERSION:2.1
FN:Campus Medical Clinic
N:Campus Medical Clinic
TEL:555-EDU-HURT
ADR:8230 Boone Blvd.;Bldg 001;;Vienna;VA;22181;
X-MS-OL-DEFAULT-POSTAL-ADDRESS:01155
EMAIL:medical@wisconsin.edu
REV:20120501T180000Z
END:VCARD

Is there any way to convert that to JSON array?

Amol
  • 33
  • 1
  • 8
  • The data is not JSON, though it's "similar". It may be a "standard" format for which there's a converter. If not, you need to write actual code to parse it and produce a List of Maps. – Hot Licks Jan 15 '15 at 02:17

1 Answers1

2

Okay, here it is...

Using this library should solve your problem: https://github.com/mangstadt/ez-vcard

--- approach using your InputStreamReader (more exactly: Reader)

VCard vcard = Ezvcard.parse(reader).first();

(where reader is of Type "Reader", which InputStreamReader is)

and export it to JSON with the following snippet:

String json = Ezvcard.writeJson(vcard).go();
Andreas
  • 5
  • 1
  • 4
Ronald Duck
  • 323
  • 1
  • 11
  • thanks for the solution, that was so simple and worked in one go. – Amol Jan 16 '15 at 01:57
  • Meanwhile I tried parsing that manually with this code as well. 'StringBuilder sb = new StringBuilder(); BufferedReader in = new BufferedReader(reader); String inputLine; sb.append("{JSON ARRAY :["); while ((inputLine = in.readLine()) != null) { if (inputLine.equalsIgnoreCase("BEGIN:VCARD")) {sb.append("{"); } if (inputLine.contains(";")) { inputLine = inputLine.replace(";", "-"); } sb.append(inputLine + ";"); if (inputLine.equalsIgnoreCase("END:VCARD")) { sb.append("},"); } sb.append("]}"); JSONObject json = new JSONObject(sb.toString()); System.out.println(json);' – Amol Jan 16 '15 at 01:58
  • I'd rather advice you to use that library, because your Snippet won't work (I've pasted it in IntelliJ, and the while-loop isn't covering the whole BufferedReader (but I think that's because of Copy'n'Pase) and I don't know the specifics of vCard but in parsing things there are always some stumbling blocks in your way where you need to absolutely know the specifics about the format and you're always lucky having a library dealing with that... – Ronald Duck Jan 16 '15 at 10:40
  • That true, using the library was quite simple and easy, instead of manually parsing same. – Amol Jan 19 '15 at 06:58
  • That ez-vcard link don't work, and I have not found it somewhere else. Can you supply the right link ? –  Nov 04 '18 at 16:45