0

Sorry if some one ask this question earlier but I am unable to find relatively.

My query is

String like this -> {"value":123,"key":abc};

Change into =>

String str = ['"','v','a','l','u','e',':','1','2','3',',','k','e','y',':','a','b','c','"']; // char array

str[0] will be " , str[1] will be v

So I want to know whether it is possible if i want to fetch value by this. let's say

str.getString("value") = 123  <- output expecting

things i tried:

1. JSONObject newStr = (JSONObject)JsonSerializer.toJSON(str);
2. Str.contains("key")
3. JSONObject newStr = new JSONObject(str);

but nothing work for me. Can any one help me with that.

Is there any way so that i can get this answer like this.

str.get("value") = 123 and str.get("key") = abc

6

1 Answer 1

2

Maybe updating the first and last element in the char array with { and } respectively.

Constructing a String from it.

Parsing it in a JSONObject and then you can fetch what you want.

Sample code:

        Character[] str = {'{', 'v','a','l','u','e',':','1','2','3',',','k','e','y',':','a','b','c','}'};
        StringBuilder sb = new StringBuilder(str.length);
        for (Character c : str)
            sb.append(c.charValue());
        String strb = sb.toString();

        JSONObject obj = new JSONObject(strb);

        System.out.print(obj.get("value"));

Output

123

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this can be one of an answer but this one is Brute force, I am looking for some optimise solution or any library..

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.