读取JSON文件(对象和数组)

时间:2017-02-12 06:20:16

标签: php arrays json

我遇到问题尝试读取json来自url的回复是这个

public class MainActivity extends Activity {

private static final String PATIENTS_LIST_URL = "http://thingspeak.com/channels/201342/field/1.json";

ArrayList<HashMap<String, String>> patientList;

 Button button;
    TextView textview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textview=(TextView) findViewById(R.id.textView);
        button = (Button) findViewById(R.id.button_get_string);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GetJsonData();
            }
        });

    }

private void GetJsonData() {
    final JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
            PATIENTS_LIST_URL, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
         //  Toast.makeText(MainActivity.this, response.toString(), Toast.LENGTH_SHORT).show();
           textview.setText(response.toString());

            try {

                JSONArray  data = response.getJSONArray("feeds");

            JSONObject eachData = data.getJSONObject(0);
            String  address=eachData.getString("field2");

             //   Toast.makeText(getApplicationContext(),
                     //   "field1::"+address, Toast.LENGTH_SHORT).show();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
                    // Snack(error.getMessage());
                }
            }
    ) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String>map = new HashMap<String,String>();
            return map;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(jsonObjReq);
}
}

我需要读取reasults结果状态代码 我试过这个但没有工作:

{"metadata":{"iserror":"false","responsetime":"0.00s"},"results":{"result":[{"Name":"testcampaign","Id":86124,"statuscode":"Success","statusmessage":"Campaign Added Successfully..","URL":"http:\/\/ufa.askyou.co.uk","MaxBid":"1.00","Cap":"None","GeoTargets":"US","BrowserTargets":"ie","UniquesOnly":"true","Schedule":"No Schedule, Free To Run"}]}}

如果我使用他的函数获取所有数据

$json = file_get_contents($url . $Params . $Params2);
$obj = json_decode($json, true);
echo $obj['results']['result']['statusmessage'];

他们说结果$obj = json_decode($data, true); foreach ($json as $key => $value) { if (!is_array($value)) { echo $key . '=>' . $value . '<br/>'; } else { foreach ($value as $key => $val) { echo $key . '=>' . $val . '<br/>'; } } } 。 我怎么能读到这个?

0 个答案:

没有答案