-1

enter image description hereI have below JsonArry

[{"Tpid":"23, 45"}] and want to convert in stringList in below format list=[23, 45] Can anyone please help? Please note that it can contain 1k list also.

  • https://stackoverflow.com/questions/2255220/how-to-parse-a-json-and-turn-its-values-into-an-array – Albert Rovsing Jun 29 '22 at 13:48
  • 1
    `{"Tpid":"23", "45"}` isn't valid JSON object since `"45"` doesn't have its *key*. Please try to avoid adding to question unrelated problems so we all wouldn't waste our time (you because you are not really interested in it, others because problem they are pointing out/trying to solve probably doesn't even exists in your real scenario). – Pshemo Jun 29 '22 at 13:49
  • @Pshemo- I have added the screen shot. – Tiwari Richa Jun 29 '22 at 14:12
  • 1
    The screenshot shows a single string containing two numbers separated by a comma. That is very different from what you show in the text. Please edit your question to show "23,45" instead of "23","45". – k314159 Jun 29 '22 at 14:22
  • Anyway, the answer is that "23,45" is returned as just a string. You can use `String.split` to split it yourself. – k314159 Jun 29 '22 at 14:23
  • Thanks @k314159, I have changed it now but I am not able to get the list in the format I mentioned. Can you please advice how to do it? – Tiwari Richa Jun 29 '22 at 15:29
  • I am using String [] arr=jsonArray.toString(). split(":") And it is giving "23, 45"}] – Tiwari Richa Jun 29 '22 at 15:31
  • Please [edit] your question to include your code and JSON as **text** rather than as a screenshot. On stack overflow images should not be used for textual content, see [*Discourage screenshots of code and/or errors*](https://meta.stackoverflow.com/a/307500) and [*Why not upload images of code on SO when asking a question*](https://meta.stackoverflow.com/a/285557) for why. For instructions on formatting see *[How do I format my code blocks?](https://meta.stackexchange.com/q/22186)*. A [mcve] showing what you have tried that did not work would maximize your chances of getting help. See [ask]. – dbc Jun 29 '22 at 15:38
  • Please take down your screenshot. Now Everyone knows the code is for Which company. The leaking of codes of a company in a public forum may not end up well if they find out. – Sayan Bhattacharya Jun 29 '22 at 16:06

1 Answers1

1

You JSonArray is [{"Tpid":"23, 45"}]. What you want to extract is "23, 45".

Your array contains one JSON object. That JSON object is at index 0 of your array.

So, first, use your JSON library to retrieve your JSON object as the first element of your JSON array.

Then, get the value from your JSON object.

And finally, take the value and split it.

Example (pseudocode depending on your JSON library):

JSONObject jsonObject = jsonArray.get(0);
String value = jsonObject.getAsString("Tpid");
String[] arrayOfValues = value.split(", ");
k314159
  • 5,051
  • 10
  • 32