如何从json Object获取特定的键值

时间:2014-07-23 10:05:34

标签: json json.net

我正在尝试从json对象获取特定的键值。我的json对象如下所示。 我怎样才能获得" pProductId "的价值?从下面的json
我想从java脚本或jquery中获取它。

JSON对象:

*pUserID*:*16*,
*pLoadRequirementID*:*3023*,
*pLoadInstanceID*:*8950*,
*pErroneous*:*false*,
*pErrorMessage*:**,
*pAuditLoginName*:*admin*,
*pProductId*:*1*

2 个答案:

答案 0 :(得分:0)

考虑这个例子。您可以使用此代码处理您的问题。这是另一个程序中使用的代码

MainActivity

package com.example.adaderana;

import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;


 public class MainActivity extends Activity  {
    // Declare Variables
    JSONObject jsonobject;
    JSONArray jsonarray;
    ListView listview;
    ListViewAdapter adapter;
    ProgressDialog mProgressDialog;
    ArrayList<HashMap<String, String>> arraylist;


    static String TITLE = "title";
    static String AUTHOR = "author";
    static String THUMBNAIL = "thumbnail";
    static String CONTENTS = "contents";
    static String IMAGE = "image";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from list_main.xml
        setContentView(R.layout.button);


        Button b = (Button) findViewById(R.id.button2);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent myIntent = new Intent(MainActivity.this, Video.class);
                startActivityForResult(myIntent, 0);

            }
        });

        Button a = (Button) findViewById(R.id.button1);

        a.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

              // TODO Auto-generated method stub
              new DownloadJSON().execute();
            }
        });
 }

    // DownloadJSON AsyncTask
    private class DownloadJSON extends AsyncTask<Void, Void, Void> {


 @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressdialog
            mProgressDialog = new ProgressDialog(MainActivity.this);
            // Set progressdialog title
            mProgressDialog.setTitle("Derana News");
            // Set progressdialog message
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            mProgressDialog.show();
        }


        @Override
        protected Void doInBackground(Void... params) {
            // Create an array
            arraylist = new ArrayList<HashMap<String, String>>();
            // Retrieve JSON Objects from the given URL address
            jsonobject = JSONfunctions
                    .getJSONfromURL("http://www.adaderana.mobi/apple/ipad.php?q=topcat&cat=36");

            try {
                // Locate the array name in JSON
                jsonarray = jsonobject.getJSONArray("TopNewsGivenCat");

                for (int i = 0; i < jsonarray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    jsonobject = jsonarray.getJSONObject(i);
                    // Retrive JSON Objects
                    map.put("title", jsonobject.getString("title"));
                    map.put("author", jsonobject.getString("author"));
                    map.put("thumbnail", jsonobject.getString("thumbnail"));
                    map.put("contents", jsonobject.getString("contents"));
                    map.put("image", jsonobject.getString("image"));

                    // Set the JSON Objects into the array
                    arraylist.add(map);
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            setContentView(R.layout.listview_main);
            // Locate the listview in listview_main.xml
            listview = (ListView) findViewById(R.id.listview);
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(MainActivity.this, arraylist);
            // Set the adapter to the ListView
            listview.setAdapter(adapter);
            // Close the progressdialog
            mProgressDialog.dismiss();
        }
    }
}

要阅读JSON数据,您还需要使用此类

package com.example.adaderana;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
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 JSONfunctions {

    public static JSONObject getJSONfromURL(String url) {
        InputStream is = null;
        String result = "";
        JSONObject jArray = null;

        // Download JSON data from URL
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
        }

        // Convert response to string
        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();
            result = sb.toString();
        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }

        try {

            jArray = new JSONObject(result);
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());
        }

        return jArray;
    }
}

答案 1 :(得分:0)

试试这个

        Dim json As IDictionary(Of String, JToken) = JObject.Parse("{your json string}")
        Dim data = json("your specified key")