Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

1 like 0 dislike
341 views
by The go-to Tester (262 points)
retagged by
Ex json : {id:1,data:{country:uk,state:cf},type:google}

i have to store this value in string[] to find the number field in json.
by The go-to Tester (181 points)
What will be use case of strong JSON into String[]?

Are you planning to create an Array of String[] and store each JSON into it?

e.g.

String[] arrStray = new String[5];
arrStray[0] = "{id:1,data:{country:uk,state:cf},type:google}";
arrStray[1] = "{id:2,data:{country:uk,state:cf},type:google}";
arrStray[2] = "{id:3,data:{country:uk,state:cf},type:google}";
arrStray[3] = "{id:4,data:{country:uk,state:cf},type:google}";
arrStray[4] = "{id:5,data:{country:uk,state:cf},type:google}";

Is that what you are looking for?
by The go-to Tester (262 points)
I want to find the number of fields in json. Once I stores those in string[] it's easy to find the count . Is any other way to do that...

I am storing values in string[] and getting count based on special characters

1 Answer

0 like 0 dislike
by The go-to Tester (181 points)

You can try below code to count number of fields. I used Googl's Gson library for this.

 

Link to Gson library on maven http://search.maven.org/#artifactdetails%7Ccom.google.code.gson%7Cgson%7C2.8.0%7Cjar

 

String strJSON = "{id:1,data:{country:uk,state:cf},type:google}";
Gson gson = new Gson();
Map<String, Object> map = gson.fromJson(strJSON, new TypeToken<Map<String, Object>>(){}.getType());
Set<String> keys = map.keySet();
System.out.println(keys.size()); //counting number of fiels
for(String key : keys)
System.out.println(key); //printing number of fiels.
 
 
Output:
3
id
data
type
 
 
Hope that helps.


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...