如何从此JSON字符串Android获取特定键的值?

时间:2013-11-16 17:00:51

标签: android json wcf get

我有来自wcf服务的结果,它返回一个JSON字符串,如下所示,

[[{"Key":"Name","Value":"Profile"},{"Key":"Icon","Value":"Assets\/MenuIcons\/ProfileRegistration"}],[{"Key":"Name","Value":"CAF"},{"Key":"Icon","Value":"Assets\/MenuIcons\/CAF"}],[{"Key":"Name","Value":"STBTOOLS"},{"Key":"Icon","Value":"Assets\/MenuIcons\/STB Tools"}]]

从这个我如何获得特定的一对名称和图标,有三个键值对。当我这样做时,它会给出一个错误,因为名称为No No值;

JSONArray obj = new JSONArray(result);      

for (int i = 0; i < obj.length(); i++) {
    JSONArray innerJsonArray = obj.getJSONArray(i);
    JSONObject jsonObject = innerJsonArray.getJSONObject(0);
    String areaID = jsonObject.getString("Name");
    String Icon = jsonObject.getString("Icon");
}

2 个答案:

答案 0 :(得分:1)

http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

您的json值映射在“Key”和“Value”中,而不是“Name”或“Icon”中 左边部分是键,右边部分是值。您正尝试使用getString方法中的值检索值。 你应该做

jsonObject.getString("Key");
jsonObject.getString("Value");

而不是: -

jsonObject.getString("Name");
jsonObject.getString("Icon");

你的Json: -

[
[
    {
        "Key": "Name",
        "Value": "Profile"
    },
    {
        "Key": "Icon",
        "Value": "Assets/MenuIcons/ProfileRegistration"
    }
],
[
    {
        "Key": "Name",
        "Value": "CAF"
    },
    {
        "Key": "Icon",
        "Value": "Assets/MenuIcons/CAF"
    }
],
[
    {
        "Key": "Name",
        "Value": "STBTOOLS"
    },
    {
        "Key": "Icon",
        "Value": "Assets/MenuIcons/STB Tools"
    }
]

答案 1 :(得分:0)

这是我的代码。我把你的json放在res目录下名为raw的文件夹中,这里是代码,它正常工作,就像我告诉你的那样: -

package com.example.test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    InputStream inputStream = getResources().openRawResource(R.raw.json);  
// Create a folder named "raw" and add a file named "json" and pasted your json code in it to test it .
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    int ctr;
    try {
        ctr = inputStream.read();
        while (ctr != -1) {
            byteArrayOutputStream.write(ctr);
            ctr = inputStream.read();
        }
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Log.v("Text Data", byteArrayOutputStream.toString());
    try {
        String json = byteArrayOutputStream.toString();
        System.out.println(json);
        JSONArray obj = new JSONArray(json);      

        for (int i = 0; i < obj.length(); i++) {
            JSONArray innerJsonArray = obj.getJSONArray(i);
            JSONObject jsonObject = innerJsonArray.getJSONObject(0);
            String areaID = jsonObject.getString("Key");
            String Icon = jsonObject.getString("Value");
        }

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

    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}