我想用json (my)解析此tutorial (source code)。
我的代码是:
public class GameScreen implements Screen {
myGame myGame;
SpriteBatch batch;
Sprite sprite;
Texture img;
Texture imgDialogBoxGood;
OrthographicCamera camera;
private Stage stage;
MainChar mainChar;
public GameScreen(myGame myGame) {
this.myGame = myGame;
camera = new OrthographicCamera();
camera.setToOrtho(false, 480, 800);
mainChar = new MainChar();
batch = new SpriteBatch();
stage = new Stage();
stage.getViewport().setCamera(camera);
stage.addActor(mainChar);
batch.setProjectionMatrix(camera.combined);
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
batch.begin();
stage .act(Gdx.graphics.getDeltaTime());
stage.draw();
batch.end();
}
@Override
public void resize(int width, int height) { }
@Override
public void dispose() {
stage.dispose();
myGame.dispose();
}
}
此代码来自androidhive,但我不知道如何解析json(my)。
那么,如何解析json?
答案 0 :(得分:6)
试试这个:
String url = "http://api.giphy.com/v1/gifs/search?q=funny+cat&api_key=dc6zaTOxFJmzC";
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jo = jsonArray.getJSONObject(i);
// Do you fancy stuff
// Example: String gifUrl = jo.getString("url");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Anything you want
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(stringRequest);