Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中获取JSON数据_Android_Json - Fatal编程技术网

如何在android中获取JSON数据

如何在android中获取JSON数据,android,json,Android,Json,我有这样的JSON数据:我对获取这些JSON数据没有什么困惑 JSONObject jb = (JSONObject)entries.getJSONObject("data"); Log.d("Geting Value---", "+----"+jb.length()); //here i got 6 length. for (int i = 0; i < jb.length(); i++) { JSONObject postL

我有这样的JSON数据:我对获取这些JSON数据没有什么困惑

JSONObject jb = (JSONObject)entries.getJSONObject("data"); 
Log.d("Geting Value---", "+----"+jb.length()); //here i got 6 length.
for (int i = 0; i < jb.length(); i++) {
JSONObject postListObj = jb.getJSONObject("1");

String Title = postListObj.getString("title");

namelist.add(Title);
}

那么,如何获取这些json数据,我想存储在列表id和标题中。我的主要目的是在列表中显示这些标题,如果我单击CXget id of clicked。在toast上显示建议删除的已单击项。(发布到已单击的web列表项)。

幸运的是,Android提供了干净且平均的实用程序来解析JSON。
如果你想解析一个给定的JSON字符串,它基本上可以归结为和

 JSONObject postListObj = jb.getJSONObject("3");
 String Title = postListObj.getString("title");
你总是得到同样的标题6次

试一试

这太容易了。这是JSON格式:

“userDB”是一个JSONArray标记,“userId”/“userName”/“userBatch”/“userDep”是一个字符串标记/JSON节点名。您可以根据需要添加字符串。将web域上的数据设置为.html或.txt文件格式

现在将这个类添加到jsonTest.jsonparsing.library或您的java包jsonparsing.java 此类帮助您读取jeson数据

现在在jsonTest.json中的MainActivity.java类中解析这个或我们的包


请详细说明你的问题。不清楚你想做什么我用你的代码时得到了。W/System.err(16017):org.json.JSONException:org.json.jsonary类型数据的值[{“ID”:1172,“title”:“dsfsdf”},{“ID”:1,“title”:“V”},{“ID”:4,“title”:“VC”},{“ID”:1172,“title”:“dsfsdf”},{“ID”:5,“title”:“F”},{JSONObject@user2332049json数组来自哪里<代码>[表示json数组node@user2332049似乎您的顶部有一个
JSONArray
而不是
JSONObject
。请发布您想要解析的更新json以获得进一步帮助。这就是为什么您有org.json.JSONArray无法转换为JSONObject的原因
 JSONObject postListObj = jb.getJSONObject("3");
 String Title = postListObj.getString("title");
    ArrayList<HashMap<String,String>> listMap= new ArrayList<HashMap<String,String>>();
    try
    {
        JSONObject entries = new JSONObject(load()) ;
        JSONObject dataObject = entries.getJSONObject("data");

        Iterator<String> keysIterator = dataObject.keys();
        while (keysIterator.hasNext()) 
        {
                HashMap<String,String> map = new HashMap<String,String>();
                String keyStr = (String)keysIterator.next();
                JSONObject postListObj = dataObject.getJSONObject(keyStr);
                int  key = postListObj.getInt("ID");
                String value = postListObj.getString("title");
                map.put(key, value);
                listMap.add(map);
                Log.i("Key is.....",""+key);
                Log.i("Value is....",value);
        }  

    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
02-18 12:42:09.757: I/Key is.....(1351): 1
02-18 12:42:09.757: I/Value is....(1351): A
02-18 12:42:09.757: I/Key is.....(1351): 8
02-18 12:42:09.767: I/Value is....(1351): Z
02-18 12:42:09.767: I/Key is.....(1351): 34
02-18 12:42:09.767: I/Value is....(1351): CX
02-18 12:42:09.767: I/Key is.....(1351): 1172
02-18 12:42:09.767: I/Value is....(1351): dsfsdf
02-18 12:42:09.767: I/Key is.....(1351): 5
02-18 12:42:09.767: I/Value is....(1351): X
02-18 12:42:09.767: I/Key is.....(1351): 2
02-18 12:42:09.767: I/Value is....(1351): B
{
"userDB": [
        {
         "userId": "UG02-00-00-000",
         "userName": "Jon",
         "userBatch": "2nd",
         "userDep": "C.S.E"
        }
    ]
}
package jsonTest.jsonparsing.library;    
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    public JSONObject getJSONFromUrl(String url) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();           

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}
package jonTest.jsonparsing;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import jsonTest.jsonparsing.library.JSONParser;


public class MainActivity extends Activity {

    //URL to get JSON Array this is the main part pass add your URL which return data, If link not working then save JSON your domain and then set URL
    private static String url = "http://yourlink";

    //JSON Node Names 
    //set TAG must same name which you use in your JSON and add tag as you want.
private static final String TAG_USER = "userDB";
private static final String TAG_ID = "roll";
private static final String TAG_NAME = "name";
private static final String TAG_BATCH= "userBatch";
    private static final String TAG_DEP = "dept";

    JSONArray user = null;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        // Creating new JSON Parser
        JSONParser jParser = new JSONParser();

        // Getting JSON from URL
        JSONObject json = jParser.getJSONFromUrl(url);

        try {
            // Getting JSON Array
            user = json.getJSONArray(TAG_USER);
            JSONObject c = user.getJSONObject(0);

        // Storing  JSON item in a Variable
        String id = c.getString(TAG_ID);
        String name = c.getString(TAG_NAME);
        String userBatch = c.getString(TAG_BATCH);
        String dep = c.getString(TAG_DEP);

            /*
            //Importing TextView just for show data in value
            final TextView uid = (TextView)findViewById(R.id.uid);
            final TextView name1 = (TextView)findViewById(R.id.name);
            final TextView batch1 = (TextView)findViewById(R.id.batch);
            final TextView dep1 = (TextView)findViewById(R.id.dep);
            //Set JSON Data in TextView
            uid.setText(id);
            name1.setText(name);
            batch1.setText(totalSub);
            dep1.setText(dep); */

    } catch (JSONException e) {
        e.printStackTrace();
    }
  }   
}