改变Json输出

时间:2011-04-28 18:31:10

标签: php

如何从数据库生成此输出。我正在使用php和mysql,并希望通过使用JSON

来输出

但我以这种格式输出

{"earthquakes": [
    {
        "eqid": "c0001xgp",
        "magnitude": 8.8,
        "lng": 142.369,
        "src": "us",
        "datetime": "2011-03-11 04:46:23",
        "depth": 24.4,
        "lat": 38.322
  },
  {
        "eqid": "2007hear",
        "magnitude": 8.4,
        "lng": 101.3815,
        "src": "us",
        "datetime": "2007-09-12 09:10:26",
        "depth": 30,
        "lat": -4.5172
  }
<--more -->
]}

所以请指导我如何更新我的代码?

我正在设置这个因为我想让它适合代码为

的Android应用程序
package com.pxr.tutorial.json;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.pxr.tutorial.xmltest.R;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class Main extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listplaceholder);

        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();


        JSONObject json = JSONfunctions.getJSONfromURL("http://midsweden.gofreeserve.com/fetch.php");

        try{

            JSONArray  earthquakes = json.getJSONArray("earthquakes");

            for(int i=0;i<earthquakes.length();i++){                        
                HashMap<String, String> map = new HashMap<String, String>();    
                JSONObject e = earthquakes.getJSONObject(i);

                map.put("id",  String.valueOf(i));
                map.put("name", "Earthquake name:" + e.getString("eqid"));
                map.put("magnitude", "Magnitude: " +  e.getString("magnitude"));
                mylist.add(map);            
            }       
        }catch(JSONException e)        {
             Log.e("log_tag", "Error parsing data "+e.toString());
        }

        ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, 
                        new String[] { "name", "magnitude" }, 
                        new int[] { R.id.item_title, R.id.item_subtitle });

        setListAdapter(adapter);

        final ListView lv = getListView();
        lv.setTextFilterEnabled(true);  
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
                @SuppressWarnings("unchecked")
                HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);                   
                Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show(); 

            }
        });
    }
}

由于

1 个答案:

答案 0 :(得分:0)

我需要的就是这些行

$rs = mysql_query("SELECT * FROM earthquakes"); 

    while($obj = mysql_fetch_object($rs)) {
            $arr[] = $obj;
        }

        echo '{"earthquakes":'.json_encode($arr).'}'