0

I want to parse my string input as a json and then get items from it for this reason i have added this code:

String data=new String(content.getBytes(),"UTF-8");
         String x=data.replaceAll("[\\n\\r\\t]+", "");
         System.out.println(x+"=================================================");
        FormatedData="id:"+id+"\n"+"name:"+name+"\n"+"comment:"+comment;
         String id,name,comment;

         JsonParser parser = new JsonParser();

              System.out.println(x+"===================================================");
                JsonObject obj = (JsonObject) parser.parse(x).getAsJsonObject();
                System.out.println(obj+"===============================================");
                  name = obj.get("organizationNameEN").getAsString();
                  id = obj.get("id").getAsString();
                  comment=obj.get("comment").getAsString();

         String FormatedData="id:"+id+"\n"+"name:"+name+"\n"+"comment:"+comment;

here is my JsonData:

{id:15, organizationNameGE:55, organizationNameEN:5656, cardNumber:6767, startDate:02/04/2018, period:5, endDate:02/10/2018, priority:not so important, $$hashKey:19A, comment:comment, mail:mail}

I am useing this library for parsing com.google.gson.JsonParser; but when i try to make this operation i got this error: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 89 path $.startDate I HVAE REMOVED ALL SPACES FROM STRING WHAT SHOULD I CHENAGE HERE TO GET RID OF THIS ERROR?

beginer beginer
  • 131
  • 1
  • 11
  • What is output of System.out.println(x+"================================================="); – Rahul Apr 02 '18 at 11:04
  • it will print my json plus this symbols "===============" – beginer beginer Apr 02 '18 at 11:05
  • i want your json data. Can you post it? – Rahul Apr 02 '18 at 11:06
  • I HAVE ADDED JSON CONTENT IN MAIN QUESTION – beginer beginer Apr 02 '18 at 11:13
  • 2
    Your posted json format is not a valid json. {id:15, organizationNameGE:55, organizationNameEN:5656, cardNumber:6767, startDate:'02/04/2018', period:5, endDate:'02/10/2018', priority:'not so important', $$hashKey:'19A', comment:'comment', mail:'mail'}\n string must be in single quote for poper json. – Rahul Apr 02 '18 at 11:18
  • you can map JSON to a POJO using GSON https://stackoverflow.com/questions/27039511/mapping-json-into-pojo-using-gson?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – utkarsh dubey Apr 02 '18 at 11:31
  • I have used this example but this error is still here com.google.gson.stream.MalformedJsonException: – beginer beginer Apr 02 '18 at 12:07
  • then your json must not be valid check it here https://jsonformatter.curiousconcept.com/ – utkarsh dubey Apr 03 '18 at 06:31

0 Answers0