如何在android中解析无键嵌套json对象

如何在android中解析无键嵌套json对象,android,json,android-fragments,gson,Android,Json,Android Fragments,Gson,请看下面的回复并帮助我 { "widget": { "debug": "on", { "title": "Sample Konfabulator Widget", "name": "main_window", "width": 500, "height": 500 }, { "src": "Ima

请看下面的回复并帮助我

{
       "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;"
        }
    }} 

您没有有效的json响应。Json按定义()是一个键值对。没有键就不能有json。

使用OkHttp API HTTP是现代网络应用的方式。这就是我们交换数据和媒体的方式。高效地使用HTTP可以加快加载速度并节省带宽。
您的数据似乎不是有效的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.
    */