1

I have string of below format.

{"id":"1tr0vm","title":"Professional business consultant in Cookeville, TN at IAG","category":"business"}

i would like to split it as

{"id":"1tr0vm "title":"Professional business consultant in Cookeville, TN at IAG" "category":"business"}

currently i am doing it this way string.split(",") but this is not working if there is ',' any where else in the string. i the above case

"Professional business consultant in Cookeville, TN at IAG"

is split into two, but need it as a whole string.

VVN
  • 1,607
  • 2
  • 16
  • 25
kiran6
  • 1,247
  • 2
  • 13
  • 19

2 Answers2

2

From your question, what i understand is that, you want to create a JSON object from a string. Try as below:

UPDATE:

Try and download the gson library from http://www.java2s.com/Code/Jar/g/Downloadgson231jar.htm and add to your libs folder and add to build path.

Then code like this

import com.google.gson.Gson;

class JsonCheck{
    public static void main(String args[]) {
        Details details = null;
        String json = "{\"id\":\"1tr0vm\",\"title\":\"Professional business consultant in Cookeville, TN at IAG\",\"category\":\"business\"}";
        Gson gsoObj = new Gson();
        details = gsoObj.fromJson(json, Details.class);
        System.out.println(details.id);
    }

    private class Details {
        private String id = "";
        private String title = "";
        private String category = "";
    }
}

Modify it like your requirements

kiran6
  • 1,247
  • 2
  • 13
  • 19
Vishnu
  • 1,516
  • 1
  • 10
  • 15
  • please can you include the complete code with the jar file used. – kiran6 Feb 19 '16 at 05:58
  • Please check the updated answer. And you dont need to use any new jar files for this. Only do the imports as shown in the answer – Vishnu Feb 19 '16 at 06:06
  • I am getting error check this http://pastie.org/10728415 for the code. I am using java.json.jar. – kiran6 Feb 19 '16 at 06:24
0

That is not a String it's a Json . if you want to convert it into a String this is how you do it

As you reading the file as a String you can convert it into Json Object and then extract the data

   JsonObject obj = new JsonObject("String extracted ");

String id = obj.getString("id");

String title = JsonObject.getString("title");

This is how you do it . I hope this was helpful , ThankYou

Sumanth Jois
  • 3,146
  • 4
  • 27
  • 42