0

I have a Java Rest API @PUT. Which is receiving the json data as shown in below format

["name1,scope1,value1","name2,scope2,value2"]

I am getting this value in my Java API method as

(String someList)

someList will contain ["name1,scope1,value1","name2,scope2,value2"]

How to get these values ("name1,scope1,value1" and "name2,scope2,value2") in String array?

CFUser
  • 2,295
  • 5
  • 32
  • 37
  • 1
    Use a JSON parser/generator. – Sotirios Delimanolis Nov 26 '13 at 13:31
  • I suppose you work with JAX-RS / Jersey? In this case, take a look on the documentation: https://jersey.java.net/documentation/latest/user-guide.html#json – kalamar Nov 26 '13 at 13:34
  • 1
    @BharathRallapalli It's a JSON array with two elements (strings). But you're right, the design looks bad. – kalamar Nov 26 '13 at 13:38
  • @BharathRallapalli That is valid json, though it'd probably be more useful use to have such a response in a format like [{"name":"name1","scope":"scope1","value":"value1"},...] as that would be ready for immediate extraction. – bgse Nov 26 '13 at 13:39

3 Answers3

1

Using the org.json package, this would do (assuming response as String in responseString):

JSONArray myJSON = new JSONArray(responseString);
String[] myValues = new String[myJSON.length];
for(int i=0; i<myValues.length; i++) {
    myValues[i] = myJSON.getString(i);
}

If you then want to split up the strings in myValues[] using ',' as a separator, you can do:

String[] innerArray = myValues[i].split(","); 
bgse
  • 8,237
  • 2
  • 37
  • 39
  • 1
    It's a good generic solution. But in the specific code posted by the OP it's an array with two string values. This values can by splited into arrays by using `myValues[i].split(",")` in order to get the "inner arrays" – kalamar Nov 26 '13 at 13:43
  • Indeed, I'll add this to the answer. – bgse Nov 26 '13 at 13:48
0

You could put the parts into an array of String with just one line:

String[] parts = someList.replaceAll("^\\[\"|\"\\]$", "").split("\",\"");

The call to replaceAll() strips off the leading and trailing JSON adornment and what's left is split on what appears between the target parts, ie ","


Note that due to the flexible nature of valid json, it would be safer to use a JSON library than this "string based" approach. Use this only if you can't use a json library.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • It works for the given string, but consider that the same JSON can be slightly different (whitespaces, linebreaks, encoded special characters, ...). Because the inputString is an IO value (constructed by a specific client) you have to take care of this. It's realy better to use a good JSON parser in this case. – kalamar Nov 26 '13 at 14:01
  • @kalamar yeah. you probably could write an ugly regex that would work for all cases, but why bother when you can just parse. I'll leave this answer here though in case someone can't use a json library, but I've added a caveat – Bohemian Nov 26 '13 at 14:09
0

An example JSON code :

import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.parser.ParseException;
import org.json.simple.parser.JSONParser;

class JsonDecodeDemo 
{
   public static void main(String[] args) 
   {
      JSONParser parser=new JSONParser();
      String s = "[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
      System.out.println("s = " + s);
      try{
         Object obj = parser.parse(s);
         JSONArray array = (JSONArray)obj;
         System.out.println("The 2nd element of array");
         System.out.println(array.get(1));
         System.out.println();

         JSONObject obj2 = (JSONObject)array.get(1);
         System.out.println("Field \"1\"");
         System.out.println(obj2.get("1"));    

         s = "{}";
         obj = parser.parse(s);
         System.out.println(obj);

         s= "[5,]";
         obj = parser.parse(s);
         System.out.println(obj);

         s= "[5,,2]";
         obj = parser.parse(s);
         System.out.println(obj);


      }catch(ParseException pe){
         System.out.println("position: " + pe.getPosition());
         System.out.println(pe);
      }
   }
}

You can refer to this.

Nishant Lakhara
  • 2,295
  • 4
  • 23
  • 46