Android无法解析json,即使它有效

时间:2016-12-02 09:53:56

标签: android json android-json

以下JSON提供错误

System.err: org.json.JSONException: Expected ':' after Incline at character 31

我已经验证了来自jsonlint&的JSON。 jsonformatter.curiousconcept并且它们都返回有效的json结果

{
"day": [{
    "monday": {
        "Chest": {
            "Flat Bench press": {
                "Sets": "3",
                "Reps": "12"
            },
            "Incline Bench press": {
                "Sets": "3",
                "Reps": "12"
            },
            "Flat Dumbell press": {
                "Sets": "3",
                "Reps": "12"
            },
            "Flat Dumbell Fly": {
                "Sets": "3",
                "Reps": "12"
            },
            "Pull Over": {
                "Sets": "3",
                "Reps": "12"
            },
            "Machine Chest Press": {
                "Sets": "3",
                "Reps": "12"
            }
        }
    }
}, {
    "tuesday": {
        "Shoulders": {
            "UpRight Row": {
                "Sets": "3",
                "Reps": "12"
            },
            "Front Press": {
                "Sets": "3",
                "Reps": "12"
            },
            "Back OverHead Press": {
                "Sets": "3",
                "Reps": "12"
            },
            "Dumbell press": {
                "Sets": "3",
                "Reps": "12"
            },
            "Single Hand Side Raise": {
                "Sets": "3",
                "Reps": "12"
            },
            "Delt Raise": {
                "Sets": "3",
                "Reps": "12"
            },
            "Rear Delt Raise": {
                "Sets": "3",
                "Reps": "12"
            },
            "Shrugs": {
                "Sets": "3",
                "Reps": "12"
            }
        }
    }
}, {
    "wednesday": {
        "Back": {
            "WideGrip UpRight Row": {
                "Sets": "3",
                "Reps": "12"
            },
            "Front Pull Ups": {
                "Sets": "3",
                "Reps": "12"
            },
            "Front Pull Down": {
                "Sets": "3",
                "Reps": "12"
            },
            "AD Side Table Rowing": {
                "Sets": "3",
                "Reps": "12"
            },
            "Ground Pulley": {
                "Sets": "3",
                "Reps": "12"
            },
            "AD Rowing": {
                "Sets": "3",
                "Reps": "12"
            },
            "Reverse Pulley": {
                "Sets": "3",
                "Reps": "12"
            }
        }
    }
}, {
    "thursday": {
        "Triceps": {
            "Short Grip Bench Press": {
                "Sets": "3",
                "Reps": "12"
            },
            "Lying Triceps": {
                "Sets": "3",
                "Reps": "12"
            },
            "French Press": {
                "Sets": "3",
                "Reps": "12"
            },
            "Barbell Overhead": {
                "Sets": "3",
                "Reps": "12"
            },
            "Pulley PushDown": {
                "Sets": "3",
                "Reps": "12"
            },
            "Back PushUps": {
                "Sets": "3",
                "Reps": "12"
            }
        }
    }
}, {
    "friday": {
        "Biceps": {
            "Barbell Curl": {
                "Sets": "3",
                "Reps": "12"
            },
            "AD Dumbell Curl": {
                "Sets": "3",
                "Reps": "12"
            },
            "Preacher Curl": {
                "Sets": "3",
                "Reps": "12"
            },
            "Machine Curl": {
                "Sets": "3",
                "Reps": "12"
            },
            "Hammer Curl": {
                "Sets": "3",
                "Reps": "12"
            }
        }
    }
}, {
    "saturday": {
        "Legs": {
            "Squats": {
                "Sets": "2",
                "Reps": "50"
            },
            "Barbell Squats": {
                "Sets": "4",
                "Reps": "15"
            },
            "Leg Press": {
                "Sets": "3",
                "Reps": "12"
            },
            "Leg Curl": {
                "Sets": "3",
                "Reps": "12"
            },
            "Calf Raise": {
                "Sets": "3",
                "Reps": "12"
            }
        }
    }
}]
}

Java代码:

我正在使用firebase将json存储为数据库,然后我将其作为String检索并转换为Jsonobject

public class StartActivity extends AppCompatActivity{

    Toolbar ToolbarStart;
    private DrawerLayout mDrawerLayout;
    ActionBarDrawerToggle mDrawerToggle;
    NavigationView mNavigationView;
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference mDatabase = database.getReference();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        initilize();
        initilizeNavigationDrawer();
        getSupportFragmentManager().beginTransaction().replace(R.id.startContainer,new GymChartFragment()).commit();
        mDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                try {
                    JSONObject obj = new JSONObject(dataSnapshot.getValue()+"");
                    JSONArray arr = obj.getJSONArray("day");
                    Log.d("Size",arr.length()+"");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

    private void initilizeNavigationDrawer() {
        mDrawerLayout = (DrawerLayout)findViewById(R.id.startDrawerLayout);
        mNavigationView = (NavigationView)findViewById(R.id.mainNaviagtionView);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, ToolbarStart, R.string.drawerOpen, R.string.drawerClose) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        mDrawerToggle.syncState();
        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.menuDrawerGymChart:
                        mDrawerLayout.closeDrawers();
                        break;
                    case R.id.menuDrawerDietChart:
                        mDrawerLayout.closeDrawers();
                        break;
                    case R.id.menuDrawerSettings:
                        mDrawerLayout.closeDrawers();
                        startActivity(new Intent(StartActivity.this,Prefrences.class));
                        break;
                }
                return false;
            }
        });
    }

    private void initilize() {
        ToolbarStart = (Toolbar) findViewById(R.id.ToolbarStart);
        setSupportActionBar(ToolbarStart);
    }

}

2 个答案:

答案 0 :(得分:1)

你的json是有效的,我设法通过解析为JsonObject来解析它。

String jsonString = loadJSONFromAsset();
    try {
        JSONObject jsonObject = new JSONObject(jsonString);
        Log.d("2#","@");
    } catch (JSONException e) {
        e.printStackTrace();
    }

}

public String loadJSONFromAsset() {
    String json = null;
    try {
        InputStream is = getAssets().open("index.json");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
    return json;
}

答案 1 :(得分:0)

// Logs-----   
 12-02 15:37:53.285 22281-22281/? E/JSON: {"monday":{"Chest":{"Flat Bench press":{"Sets":"3","Reps":"12"},"Incline Bench press":{"Sets":"3","Reps":"12"},"Flat Dumbell press":{"Sets":"3","Reps":"12"},"Flat Dumbell Fly":{"Sets":"3","Reps":"12"},"Pull Over":{"Sets":"3","Reps":"12"},"Machine Chest Press":{"Sets":"3","Reps":"12"}}}}
    12-02 15:37:53.285 22281-22281/? E/JSON: {"tuesday":{"Shoulders":{"UpRight Row":{"Sets":"3","Reps":"12"},"Front Press":{"Sets":"3","Reps":"12"},"Back OverHead Press":{"Sets":"3","Reps":"12"},"Dumbell press":{"Sets":"3","Reps":"12"},"Single Hand Side Raise":{"Sets":"3","Reps":"12"},"Delt Raise":{"Sets":"3","Reps":"12"},"Rear Delt Raise":{"Sets":"3","Reps":"12"},"Shrugs":{"Sets":"3","Reps":"12"}}}}
    12-02 15:37:53.285 22281-22281/? E/JSON: {"wednesday":{"Back":{"WideGrip UpRight Row":{"Sets":"3","Reps":"12"},"Front Pull Ups":{"Sets":"3","Reps":"12"},"Front Pull Down":{"Sets":"3","Reps":"12"},"AD Side Table Rowing":{"Sets":"3","Reps":"12"},"Ground Pulley":{"Sets":"3","Reps":"12"},"AD Rowing":{"Sets":"3","Reps":"12"},"Reverse Pulley":{"Sets":"3","Reps":"12"}}}}
    12-02 15:37:53.285 22281-22281/? E/JSON: {"thursday":{"Triceps":{"Short Grip Bench Press":{"Sets":"3","Reps":"12"},"Lying Triceps":{"Sets":"3","Reps":"12"},"French Press":{"Sets":"3","Reps":"12"},"Barbell Overhead":{"Sets":"3","Reps":"12"},"Pulley PushDown":{"Sets":"3","Reps":"12"},"Back PushUps":{"Sets":"3","Reps":"12"}}}}
    12-02 15:37:53.285 22281-22281/? E/JSON: {"friday":{"Biceps":{"Barbell Curl":{"Sets":"3","Reps":"12"},"AD Dumbell Curl":{"Sets":"3","Reps":"12"},"Preacher Curl":{"Sets":"3","Reps":"12"},"Machine Curl":{"Sets":"3","Reps":"12"},"Hammer Curl":{"Sets":"3","Reps":"12"}}}}
    12-02 15:37:53.285 22281-22281/? E/JSON: {"saturday":{"Legs":{"Squats":{"Sets":"2","Reps":"50"},"Barbell Squats":{"Sets":"4","Reps":"15"},"Leg Press":{"Sets":"3","Reps":"12"},"Leg Curl":{"Sets":"3","Reps":"12"},"Calf Raise":{"Sets":"3","Reps":"12"}}}}

--/




//-----------code

  try {
            JSONObject jObject=new JSONObject(str);

            JSONArray jsonArray=jObject.getJSONArray("day");
            for(int i=0;i<jsonArray.length();i++){
                Log.e("JSON",jsonArray.getString(i));


            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
//------------simply working ,you are messing in code i think