23

How do you convert this String into gson.JsonArray?

String s= "[["110917    ", 3.0099999999999998, -0.72999999999999998, 2.8500000000000001, 2.96, 685.0, 38603.0], ["110917    ", 2.71, 0.20999999999999999, 2.8199999999999998, 2.8999999999999999, 2987.0, 33762.0]]";

This is my Code:

 com.google.gson.*;
 public static void main(String[] args)
   {
       //Declared S here
       System.out.println("String to Json Array Stmt");
       JsonParser parser = new JsonParser();
       JsonElement tradeElement = parser.parse(s.toString());
       JsonArray trade = tradeElement.getAsJsonArray();
       System.out.println(trade);
    }

Is this the way to convert this Collections string to JSonArray?

1
  • com.google.gson.JsonArray ;Library i am using this (gson)
    – Nava
    Commented Jun 23, 2011 at 13:52

3 Answers 3

47

To have a string value inside your JSON array, you must remember to backslash escape your double-quotes in your Java program. See the declaration of s below.

String s = "[[\"110917       \", 3.0099999999999998, -0.72999999999999998, 2.8500000000000001, 2.96, 685.0, 38603.0], [\"110917    \", 2.71, 0.20999999999999999, 2.8199999999999998, 2.8999999999999999, 2987.0, 33762.0]]";

Your code in the main() method works fine. Below is just a minor modification of your code in the main() method.

System.out.println("String to Json Array Stmt");
JsonParser parser = new JsonParser();
JsonElement tradeElement = parser.parse(s);
JsonArray trade = tradeElement.getAsJsonArray();
System.out.println(trade);

Lastly, remember to prefix your statement "com.google.gson.*" with the keyword "import", as shown below.

import com.google.gson.*;
3
  • Here typo mistaken for import in the question .actually added in source..thanks
    – Nava
    Commented Jun 23, 2011 at 16:07
  • Is it Possible to split the JsonArray?
    – Nava
    Commented Jun 23, 2011 at 16:43
  • You mean getting the first element ([\"110917 \", 3.0099999999999998, -0.72999999999999998, 2.8500000000000001, 2.96, 685.0, 38603.0]) of JsonArray "trade"? Try using "trade.get(0)".
    – AaronYC
    Commented Jun 24, 2011 at 6:19
11

I don't see the problem. This code runs fine for me:

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;


public class GsonExample {
    public static void main(String[] args) {
        String s= "[[\"110917\", 3.0099999999999998, -0.72999999999999998," +
                "2.8500000000000001, 2.96, 685.0, 38603.0], [\"110917\", 2.71," +
                "0.20999999999999999, 2.8199999999999998, 2.8999999999999999," +
                "2987.0, 33762.0]]";


        JsonParser  parser = new JsonParser();
        JsonElement elem   = parser.parse( s );

        JsonArray elemArr = elem.getAsJsonArray();
        System.out.println( elemArr );
    }
}

The only problem maybe is that you failed to properly escape the double quotes in your s string literal.

4
  • May i know why here we need to add /(backward slash) in front of strings?
    – Nava
    Commented Jun 23, 2011 at 16:14
  • Just I want first list only as a json array? Is it possible to pass?
    – Nava
    Commented Jun 23, 2011 at 16:43
  • The slash is to escape the " character. If you don't do it the compiler can't know you're not intending to end the string for example with a closing ". See here: en.wikipedia.org/wiki/Escape_character
    – Mike Kwan
    Commented Jun 23, 2011 at 17:01
  • elemArr.get( index ) can be used to return the first element of the array for example.
    – Mike Kwan
    Commented Jun 24, 2011 at 10:30
4

The above answers are deprecated. We have to call the static method instead. Here is the working code.

JsonArray outputJsonArray = JsonParser.parseString(pasteYourStringHere).getAsJsonArray();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.