如何在没有android的情况下解析嵌套的json对象

时间:2017-01-22 06:35:40

标签: android json android-fragments gson

请查看以下回复并帮助我

{
       "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 个答案:

答案 0 :(得分:1)

您没有有效的json响应。根据定义,Json(Wikipedia)是一个键值对。你没有没有钥匙的json。

答案 1 :(得分:0)

使用OkHttp API HTTP是现代应用网络的方式。这就是我们交换数据的方式。媒体。有效地执行HTTP可以加快您的负载并节省带宽。 http://square.github.io/okhttp/

答案 2 :(得分:0)

您的数据似乎不是有效的json数据。 但是如果你想获得每个键中的值,下面的代码将有所帮助。它只适用于您提供的结构中的数据。

   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.
    */
相关问题