power_settings_new

How to store json into string[]

by (262 points)
+1 vote
133 views
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 (171 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 (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

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

1 Answer

by (171 points)
0 votes

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.
add
...