【问题标题】:Android Application Parse json dataAndroid 应用程序解析 json 数据
【发布时间】:2014-05-06 08:25:57
【问题描述】:

你好,我有这个代码我想把这个数据解析为对象现在我得到一个任意类型的字符串

我想分别获取描述对象纬度对象和经度对象

@SuppressLint("NewApi") 
public class MainActivity extends Activity {
private static final String SOAP_ACTION = "http://tempuri.org/xxxx/xxxx";
private static final String METHOD_NAME = "xxxxxx";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://10.0.2.2:52564/xxxx.svc/soap";    

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy); 

    TextView textView = new TextView(this);

    setContentView(textView);

    SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    HttpTransportSE httpTransport = new HttpTransportSE(URL);

    try
    {
        httpTransport.call(SOAP_ACTION, envelope);  
        Object result = (Object)envelope.getResponse();
        textView.setText(result.toString());
    }
    catch (Exception exception)
    {
        textView.setText(exception.toString());
    }
}
}

【问题讨论】:

  • 看来您使用的是SOAP而不是json解析,您似乎需要进行SOAP解析
  • 如果您将响应作为 json 字符串而不是请显示 json 数据
  • 我只是将 json soap 响应解析为 JSONObject 然后提取你想要的值。显然,您必须展示一个关于如何提取值的示例的 json 示例
  • 您好,答案已更新,请查看
  • 对此还有什么帮助吗??

标签: android parsing soap android-json


【解决方案1】:

将 json 解析为对象的最简单方法是使用对象映射器

    ObjectMapper om = new ObjectMapper();
    YourClass object = om.readValue(jsonText, YourClass.class); 

这个对象映射器来自杰克逊:

http://fasterxml.github.io/jackson-databind/javadoc/2.0.0/com/fasterxml/jackson/databind/ObjectMapper.html

【讨论】:

    【解决方案2】:

    您可以只使用 json 对象来处理响应。

    以下是代码摘录:

    JSONObject obj = new JSONObject(envelope.getResponse().toString());
    String Latitude = obj.getString("Latitude");
    String Longitude = obj.getString("Longitude");
    

    如果你的 json 看起来像这样:

    {
      Latitude: "1.00",
      Longitude: "1.00"
    }
    

    根据您的评论,您的 json 如下所示:

    [
      {
        "Description": "1",
        "Latitude": 0.369,
        "Longitude": 1.258
      },
      {
        "Description": "2",
        "Lati‌​tude": 1.369,
        "Longitude": 3.258
      },
      {
        "Description": "3",
        "Latitude": 2.369,
        "Longitude": 2.258
      }
    ]
    

    你可以像这样提取值:

    JSONArray obj = new JSONArray(envelope.getResponse().toString());
    String[][] values = new String[obj.length()][3];
    for(int i = 0; i < obj.length(); i++)
    {
        values[i][0] = obj.getJSONObject(i).getString("Description");
        values[i][1] = obj.getJSONObject(i).getString("Latitude");
        values[i][2] = obj.getJSONObject(i).getString("Longitude");
    }
    

    这里可能有错误,我只是在这里快速写下。
    但这是您需要了解的概念。

    【讨论】:

    • [{"Description":"1","Latitude":0.369,"Longitude":1.258},{"Description":"2","Latitude":1.369,"Longitude": 3.258},{"Description":"3","Latitude":2.369,"Longitude":2.258}] 这是我的网络服务
    • 我得到错误值 anyType 类型的 java.lang.String 无法转换为 JSONObject
    • 您的json可能无效,您可以将您的json粘贴到本网站查看:jsonviewer.stack.hu
    • [{"Description":"1","Latitude":0.369,"Longitude":1.258},{"Description":"2","Latitude":1.369,"Longitude": 3.258},{"Description":"3","Latitude":2.369,"Longitude":2.258}] 这是我的 json,但我仍然得到 java.lang.string 类型的任何类型的异常值无法转换为 JSONOBJECT
    • 数组中的最后一个值 "Longitude" 如果我从评论中复制它,则它不是有效的 json。我不确定为什么它无效。但如果我将2.‌​258 替换为任何其他数字,则它是有效的。
    【解决方案3】:

    因为你的 json 数据是

    [
        {
            "Description": "1",
            "Latitude": 0.369,
            "Longitude": 1.258
        },
        {
            "Description": "2",
            "Latitude": 1.369,
            "Longitude": 3.258
        },
        {
            "Description": "3",
            "Latitude": 2.369,
            "Longitude": 2.258
        }
    ]
    

    为数据创建一个类,如下所示

    public class Detail {
        String description;
        Double lat;
        Double longi;
    
        public Detail(String description, Double lat, Double longi) {
            this.description=description;
            this.lat=lat;
            this.longi=longi;
        }
    }
    

    在它为Deatail创建一个arraylist之后,如下所示

    List<Detail> detailList=new ArrayList<Detail>();
    

    然后执行以下操作

    String jsondata = loadJSONFromAsset();//load data and assign as json string
            JSONArray primaryArray;
            try {
                primaryArray = new JSONArray(jsondata);
    
                for (int i = 0; i < primaryArray.length(); i++) {
    
                    JSONObject jObject = primaryArray.getJSONObject(i);
                    String description = (String) jObject.getString("Description");
                    Log.d("Description", description);
                    Double latitude =  jObject.getDouble("Latitude");
                    Log.d("Latitude", latitude+"");
                    Double longitude = jObject.getDouble("Longitude");
                    Log.d("Longitude", longitude+"");
    
                    detailList.add(new Detail(description, latitude, longitude));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    

    现在您可以从 arraylist 中获取所有详细数据

    【讨论】:

    • String jsondata = loadJSONFromAsset();//加载数据并分配为 json 字符串 我应该在这里使用什么...对不起我是新来的 谢谢
    • 你需要在这里传递 json 字符串,使该方法从那里返回 json 字符串!!
    • 你能解释一下我该怎么做吗??
    • 请获取json数组作为result.toString()并使用!!
    猜你喜欢
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多