0

Please look at below response and help me out

{
       "widget": {
        "debug": "on",
         {
            "title": "Sample Konfabulator Widget",
            "name": "main_window",
            "width": 500,
            "height": 500
        },
        { 
            "src": "Images/Sun.png",
            "name": "sun1",
            "hOffset": 250,
            "vOffset": 250,
            "alignment": "center"
        },
        {
            "data": "Click Here",
            "size": 36,
            "style": "bold",
            "name": "text1",
            "hOffset": 250,
            "vOffset": 100,
            "alignment": "center",
            "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
        }
    }} 

3 Answers 3

1

You don't have a valid json response. Json by definition (Wikipedia) is a key-value pair. You can't have a json without keys.

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

Comments

0

Use OkHttp API HTTP is the way modern applications network. It’s how we exchange data & media. Doing HTTP efficiently makes your stuff load faster and saves bandwidth. http://square.github.io/okhttp/

Comments

0

Your data doesn't seem like a valid json data. But if you want to get the values in each key, below code will help. It will only work for the data in the structure you gave.

   String json = "{\"widget\":{\"debug\":\"on\",{\"title\":\"SampleKonfabulatorWidget\",\"name\":\"main_window\",\"width\":500,\"height\":500},{\"src\":\"Images/Sun.png\",\"name\":\"sun1\",\"hOffset\":250,\"vOffset\":250,\"alignment\":\"center\"},{\"data\":\"ClickHere\",\"size\":36,\"style\":\"bold\",\"name\":\"text1\",\"hOffset\":250,\"vOffset\":100,\"alignment\":\"center\",\"onMouseUp\":\"sun1.opacity=(sun1.opacity/100)*90;\"}}}";
    json = json.substring(json.indexOf("{")+1,json.lastIndexOf("}"));
    json = json.substring(json.indexOf("{")+1,json.lastIndexOf("}"));

    // Get the value of debug key in debug variable
    String debug = json.substring(json.indexOf(":\"")+2,json.indexOf("\","));

    json = json.substring(json.indexOf("{"),json.lastIndexOf("}")+1);
    //Get the first section containing keys title,name,width,height
    String section1 = json.substring(json.indexOf("{"),json.indexOf("}")+1);
    String sec1_title = section1.substring(section1.indexOf(":")+2,section1.indexOf("\","));
    section1 = section1.substring(section1.indexOf("name\""));
    String sec1_name = section1.substring(section1.indexOf(":")+2,section1.indexOf("\","));
    section1 = section1.substring(section1.indexOf("width\""));
    String sec1_width = section1.substring(section1.indexOf(":")+1,section1.indexOf(","));
    section1 = section1.substring(section1.indexOf("height\""));
    String sec1_height = section1.substring(section1.indexOf(":")+1,section1.indexOf("}"));


    //Get the second section containing keys src,name,hOffset,vOffset,alignment
    String section2 = json.substring(json.indexOf(section1)+section1.length()+1);
    section2 = section2.substring(0,section2.indexOf("}")+1);
    String sec2_src = section2.substring(section2.indexOf(":")+2,section2.indexOf("\","));
    section2 = section2.substring(section2.indexOf("name\""));
    String sec2_name = section2.substring(section2.indexOf(":")+2,section2.indexOf("\","));
    section2 = section2.substring(section2.indexOf("hOffset\""));
    String sec2_hOffset = section2.substring(section2.indexOf(":")+1,section2.indexOf(","));
    section2 = section2.substring(section2.indexOf("vOffset\""));
    String sec2_vOffset = section2.substring(section2.indexOf(":")+1,section2.indexOf(","));
    section2 = section2.substring(section2.indexOf("alignment\""));
    String sec2_alignment = section2.substring(section2.indexOf(":")+2,section2.indexOf("\"}"));

    /*
        sec2_src has value for key src
        sec2_name has value for key name
        sec2_hOffset has value for key hOffset
        sec2_vOffset has value for key vOffset
        sec2_alignment has value for key alignment
     */

    //Get the third section containing keys data,size,style,hOffset,vOffset,alignment,onMouseUp
    String section3 = json.substring(json.indexOf(section2)+section2.length()+1);
    section3 = section3.substring(0,section3.indexOf("}")+1);
    /* Now section3 variable has the following data
        {"data":"ClickHere","size":36,"style":"bold","name":"text1","hOffset":250,"vOffset":100,"alignment":"center","onMouseUp":"sun1.opacity=(sun1.opacity/100)*90;"}

       You can write the code yourself to get required data.
    */

Comments

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.