首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改Json输出

更改Json输出
EN

Stack Overflow用户
提问于 2011-04-29 02:31:10
回答 1查看 167关注 0票数 0

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

但我的输出格式如下

代码语言:javascript
复制
{"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应用程序,它的代码是

代码语言:javascript
复制
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(); 

            }
        });
    }
}

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-29 05:08:17

我所需要的就是这套台词

代码语言:javascript
复制
$rs = mysql_query("SELECT * FROM earthquakes"); 

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5823318

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档