changing the Json output
How can i produce this output from database . I am using php and mysql and want this output by using JSON
but i output in this format
{"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 -->
]}
so please guide me how can i update my code ??
i am setting this because i want to fit it for the android app whose code is
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 = js开发者_C百科on.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();
}
});
}
}
Thanks
All i need was these set of line
$rs = mysql_query("SELECT * FROM earthquakes");
while($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;
}
echo '{"earthquakes":'.json_encode($arr).'}'
;
精彩评论