【问题标题】:Sending JSON Object from android从android发送JSON对象
【发布时间】:2015-11-26 05:49:13
【问题描述】:

我需要从 android 发送一个 JSON 对象,我应该发送的格式是:

{
  "type": "sample string 1",
  "features": [
    {
      "type": "sample string 1",
      "properties": {
        "fulcrum_id": "sample string 1",
        "fulcrum_parent_id": "sample string 2",
        "fulcrum_record_id": "sample string 3",
        "version": "sample string 4",
        "created_at": "sample string 5",
        "updated_at": "sample string 6",
        "created_by": "sample string 7",
        "updated_by": "sample string 8",
        "form010058": "sample string 9",
        "form010059": "sample string 10",
        "form010060": "sample string 11",
        "form010060_other": "sample string 12"
      },
      "geometry": "sample string 2"
    },
    {
      "type": "sample string 1",
      "properties": {
        "fulcrum_id": "sample string 1",
        "fulcrum_parent_id": "sample string 2",
        "fulcrum_record_id": "sample string 3",
        "version": "sample string 4",
        "created_at": "sample string 5",
        "updated_at": "sample string 6",
        "created_by": "sample string 7",
        "updated_by": "sample string 8",
        "form010058": "sample string 9",
        "form010059": "sample string 10",
        "form010060": "sample string 11",
        "form010060_other": "sample string 12"
      },
      "geometry": "sample string 2"
    },
    {
      "type": "sample string 1",
      "properties": {
        "fulcrum_id": "sample string 1",
        "fulcrum_parent_id": "sample string 2",
        "fulcrum_record_id": "sample string 3",
        "version": "sample string 4",
        "created_at": "sample string 5",
        "updated_at": "sample string 6",
        "created_by": "sample string 7",
        "updated_by": "sample string 8",
        "form010058": "sample string 9",
        "form010059": "sample string 10",
        "form010060": "sample string 11",
        "form010060_other": "sample string 12"
      },
      "geometry": "sample string 2"
    }
  ]
}

现在,我没有办法编写这种类型的 JSON 对象。 我想知道我是否必须组成一个 JSON 数组,然后将每个条目一个接一个地放置,或者我是否可以直接获取具有此数据的对象并将其传递给 JSON:

JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("type", obj);

请指导我编写这种类型的 JSONObject,因为我最近学习了 JSON Web 服务,但没有得到一个逻辑来塑造这个复杂的对象。

【问题讨论】:

  • 试试 Gson - github.com/google/gson
  • 你能不能给我一个java类,它可以塑造这种对象并在获得对象时返回。
  • 创建一个包含这些字段(类型、特征等)的类。字段可以是任何类型的对象,如字符串、列表或自定义对象。然后使用 Gson 库将您的类的实例转换为 json(@Kedi 为您提供了链接)
  • @Shayan_Aryan 你能解释一下上面提到的 JSON 对象吗?因为据我所知,json 是键值对的组合,但这里的事情似乎很棘手。
  • 查看我在下面添加的答案

标签: android json web-services jsonobject


【解决方案1】:

按原样应用此代码,您将获得所需的准确输出。

JSONObject jObj = new JSONObject();

    try {
        jObj.put("type", "sample string 1");

        JSONArray jArray = new JSONArray();
        for(int i=0;i<3;i++) {

            //creating jsonobject for particular jsonarray index
            JSONObject inObj = new JSONObject();
            inObj.put("type", "sample string 1");

            //creating another jsonobject into jsonarray index
            JSONObject nestedInObj = new JSONObject();

            nestedInObj.put("fulcrum_id", "sample string 1");
            nestedInObj.put("fulcrum_parent_id", "sample string 2");
            nestedInObj.put("fulcrum_record_id", "sample string 3");
            nestedInObj.put("version", "sample string 4");
            nestedInObj.put("created_at", "sample string 5");
            nestedInObj.put("updated_at", "sample string 6");
            nestedInObj.put("created_by", "sample string 7");
            nestedInObj.put("updated_by", "sample string 8");
            nestedInObj.put("form010058", "sample string 9");
            nestedInObj.put("form010059", "sample string 10");
            nestedInObj.put("form010060", "sample string 11");
            nestedInObj.put("form010060_other", "sample string 12");

            // now putting nestedInObj into JSONobject for jsonarray index
            inObj.put("properties", nestedInObj);

            inObj.put("geometry", "sample string 2");

            // adding JSONobject to jsonarray index
            jArray.put(inObj);
        }

        //putting whole JSONarray to main jsonobjct
        jObj.put("features", jArray);

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

    Log.e("json", jObj.toString());

如果有任何困难请告诉我,我很乐意提供帮助。

编码愉快!

【讨论】:

  • @Safeer 如果这有帮助,您可以将其标记为答案,以便其他人可以从相同的查询中获得启发。
  • 非常感谢,它帮助了我。实际上,我对我现在得到的 JSON 的结构感到困惑。非常感谢你。 :)
【解决方案2】:

你基本上想向服务器发送一个 Json 字符串,所以我认为以下过程可以解决你的问题:

1.将此jar添加到您的依赖项下的build.gradle文件中:

'com.google.code.gson:gson:2.3.1'
  1. 现在添加创建一个具有您想要的字段名称的类,并且您可以将这些对象转换为您发布的 json 字符串格式:

您可以通过以下方式将Objects转换为json字符串:

假设您创建了一个名为 Example.Java 的模型类,并且在您的代码中拥有该对象

Example example = new Example();

现在您可以将示例对象转换为 json,如下所示:

Gson gson = new Gson();
gson.fromJson(example , Example .class);

要将json字符串转换为相应的对象,您可以使用this

如果您仍有疑问,请在下方评论

【讨论】:

  • 你能解释一下上面提到的 JSON 对象吗,因为我只知道使用 Json 发送键值对。
【解决方案3】:

你的班级应该是这样的:

public class CustomClass{
   String type;
   List<Feature> features;

   class Feature{
      String type;
      Properties properties;
      String geometry;
   }

   class Properties{
      String fulcrum_id;
      String fulcrum_parent_id;
      String fulcrum_record_id;
      .
      .
      .
   }
}

创建它的一个实例,输入你想要的值,然后使用Gson 库将其转换为 Json,如下所示:

String json = new Gson().toJson(myObject)

【讨论】:

    猜你喜欢
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-17
    • 2013-10-15
    • 2015-07-07
    相关资源
    最近更新 更多